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