edy / redux-persist-transform-filter

Filter transformator for redux-persist
MIT License
191 stars 21 forks source link

Big performance issue #31

Open magrinj opened 4 years ago

magrinj commented 4 years ago

I was using this library to blacklist a part of my reducer state. This lead in big performances issues, using a simple blacklist filter like this one:

export const blackListEntitiesFilter = createBlacklistFilter('entities', [
  'activities',
  'photos',
  'users',
]);

With this filter, app became really slow ! The amount of datas is equivalent to 33kb.

So I use this custom filter that is doing the same:

  createTransform(
    // inbound
    (inboundState, key) => {
      return inboundState
        ? omit(inboundState, ['activities', 'photos', 'users'])
        : inboundState;
    },

    // outbound
    (outboundState, key) => {
      return outboundState
        ? omit(outboundState, ['activities', 'photos', 'users'])
        : outboundState;
    },

    {whitelist: ['entities']},
  ),

And the app works well for now, maybe it's related to the cloneDeep of this library when the state grow. This library should use pick and omit from lodash, as this two functions are working with path and avoid cloning state !

andrekovac commented 3 years ago

@magrinj Thanks for your solution!

Another solution without 3-rd party libraries (via the spread operator):

createTransform(
  // hydration (inbound)
  (inboundState, key) => {
    const { activities, photos, users, ...restOfInboundState } = inboundState;
    return restOfInboundState;
  },
  // rehydration (outbound)
  (outboundState, key) => {
    return { ...outboundState };
  },
  { whitelist: ['entities'] }
);
chrisharrison commented 2 years ago

Why would you return { ...outboundState } rather than just return outboundState?