DEV Community

Discussion on: useReducer() Hook: Let me dispatch it

 
martinpham profile image
Martin Pham • Edited

1) First, it was very clear when I mentioned the main different thing between your solution and the OP's, is the switch vs object literal.
If you're still trying to say the main differences are the arrow function, spread, destructuring,.. things (which OP already used!) … I have nothing to say. It's a useless argument because you will always try to find a non-sense reason. Feel free to laugh anyway.

2)
a) In the OP's solution, there will be a jump table, and it will jump to the correct part to run (destructuring the state, merging with new count value)
b) In your solution, you will always create the object, plus, it will destructure the state + merge the count value 3 times (for Increment, Decrement, and Reset key).

Want to see performance? (Btw if you understand it well, you don't even need to benchmark it lol)

  • 1 dispatch: OP - 100%, Yours - 46%
  • 2 dispatches: OP - 100%, Yours - 42%
  • 5 dispatches: OP - 100%, Yours - 37%

Yours is even worse in the case in which the next state is null, nullish coalescing will make the next state = previous state instead.