unadlib / mutative

Efficient immutable updates, 2-6x faster than naive handcrafted reducer, and more than 10x faster than Immer.
http://mutative.js.org/
MIT License
1.58k stars 18 forks source link

Proposal: support return values in the draft function #3

Closed unadlib closed 1 year ago

unadlib commented 1 year ago

https://github.com/reduxjs/redux-toolkit/pull/3074

If support return values in the draft function, this would mean that Mutative would need to determine if the value is a draft, and do a deep traversal of non-draft return values, and such an unpredictable return value would waste a considerable amount of performance.

But the community wants mutative to support it. The good thing is that Mutative makes a lot of performance improvements, and it doesn't lose any performance as long as we suggest trying to return the draft itself as much as possible in usage scenarios like Redux's reducer. Even if the returned value is not a draft, it doesn't have significant performance loss and we will have performance benchmarks to track it.

unadlib commented 1 year ago

Suggestion from https://github.com/reduxjs/redux-toolkit/pull/3074#issuecomment-1379334527

Since the user may refer to the draft in the return of a completely new object, I will use the option strict, which we will suggest the user enable strict in development mode and disable strict in production mode. This will ensure safe returns and also keep good performance in the production build.

If the return value is mixed drafts or undefined, then use safeReturn() wrapper.