Skip to content

React-immutable-state-invariant Slows Performance #415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
cole-robertson opened this issue Mar 9, 2020 · 9 comments
Closed

React-immutable-state-invariant Slows Performance #415

cole-robertson opened this issue Mar 9, 2020 · 9 comments

Comments

@cole-robertson
Copy link

Does Redux toolkit automatically come with the package React-immutable-state-invariant? I am getting terrible performance updating my state with some large objects since it seems that React-immutable-state-invariant has a detect mutations function that looks through all the the keys of this object multiple times.

I do not want to use this package. Looking at the github it seems to say that you should not use this in production. I don't want to even use it in development since Redux toolkit has immer for direct state mutations anyway.

How can I configure Redux Toolkit to not use this middleware? Thank you

@cole-robertson
Copy link
Author

I see #412
Is there not a current way to configure middleware right now?

@markerikson
Copy link
Collaborator

markerikson commented Mar 9, 2020

Just answered this on Reddit, but I'll answer here as well:

Yes, we use immutable-state-invariant by default, in development mode only. And yes, it is a deep check of your entire state, so if you have a rather large state, that will slow things down. Ditto for the serializable check middleware.

Per the docs, you can disable either of the dev check middleware by using getDefaultMiddleware() options.

@phryneas
Copy link
Member

phryneas commented Mar 9, 2020

See the docs for configureStore - you can use different middlewares.

const store = configureStore({
  reducer: {/*...*/},
  middleware: [...getDefaultMiddleware({immutableCheck: false})]
})

@itsramiel
Copy link

Is there any place in the docs where it is mentioned that these development middlewares affect performance during dev. I was so upset about the performance of my react native app, so I dispatched an action that does nothing and I was able to see that it was still slow. I remembered something about additional development middlewares added, so I turned them off and suddenly it is much faster.

This really need to be somewhere in the docs

@phryneas
Copy link
Member

If the development middlewares take longer than 32ms per action, they will print warnings to your console.log.
Did you ignore that?

@itsramiel
Copy link

// Ignores redux warning about serialization took a long time
LogBox.ignoreLogs(["SerializableStateInvariantMiddleware"]);

Yup there it is. My bad

@alexisgahon
Copy link

Update: getDefaultMiddleware ne s'importe plus.

const store = configureStore({
  reducer: {/*...*/},
  middleware: (getDefaultMiddleware) => [...getDefaultMiddleware({immutableCheck: false})]
})

@reduxjs reduxjs deleted a comment from atultiwaree Jul 5, 2023
@balsick
Copy link

balsick commented Dec 28, 2023

Update: getDefaultMiddleware ne s'importe plus.

const store = configureStore({
  reducer: {/*...*/},
  middleware: (getDefaultMiddleware) => [...getDefaultMiddleware({immutableCheck: false})]
})

In case of typescript, what worked for me is this:

middleware: getDefaultMiddleware =>
    getDefaultMiddleware({immutableCheck: false}),

@EskiMojo14
Copy link
Collaborator

yep - the spread is unnecessary, and in RTK 2.0 Typescript users are now required to use a Tuple instance. If you want to add more middlewares to the defaults, you should use the provided concat and prepend methods from the returned Tuple instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants