Redux: Switch string comparison is expensive (over 250)

Created on 22 Sep 2017  Â·  1Comment  Â·  Source: reduxjs/redux

Since string comparisons in switch statement are expensive, why not compare references instead?

If my understanding is wrong, please let me know.

strings:

one() {
  dispatch({ type: 'ONE', payload: 1})
}

references:

export one() {
  dispatch(one, 1)
}

then instead of

switch (action.type) {
  case 'ONE': ...
  case 'TWOTWO': ...
}

this

import {actions} from './actions'
switch (action) {
  case actions.one: ...
  case actions.twoTwo: ...

Most helpful comment

This is entirely up to your action creators and reducers. But string comparison won't be a serious bottleneck compared to other things going on with your reducers re-building state and your eventual display logic running. You're doing a premature optimization.

>All comments

This is entirely up to your action creators and reducers. But string comparison won't be a serious bottleneck compared to other things going on with your reducers re-building state and your eventual display logic running. You're doing a premature optimization.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ilearnio picture ilearnio  Â·  3Comments

caojinli picture caojinli  Â·  3Comments

rui-ktei picture rui-ktei  Â·  3Comments

olalonde picture olalonde  Â·  3Comments

mickeyreiss-visor picture mickeyreiss-visor  Â·  3Comments