0

I have tried researching this but was rather swamped, was wondering if someone has a solid answer regarding use of named functions in redux action creators vs named functions - is there any performance difference? Or any other factors that affect this?

eg:

function getUserIdentity() {
  return (dispatch) => {
    dispatch({
      type: types.GET_USER_IDENTITY,
    });
  }
}

vs

const getUserIdentity = () => (dispatch) => { dispatch({type: types.GET_USER_IDENTITY}) };

Thanks!

anthonyhumphreys
  • 1,051
  • 2
  • 12
  • 25

1 Answers1

1

Any performance difference doesn't matter, the two functions aren't even doing the same. The arrow function "equivalent" of your function declaration would be

const getUserIdentity = () => (dispatch) => { dispatch({type: types.GET_USER_IDENTITY}) };

not

const getUserIdentity = (dispatch) => dispatch({ type: types.GET_USER_IDENTITY });

as in your question.


Regarding the updated question, no there is no performance difference between calling the different function types. However, there is still a behavioural difference, see Arrow function vs function declaration / expressions: Are they equivalent / exchangeable? and also var functionName = function() {} vs function functionName() {} - a variable initialisation happens at a different time than that of a "hoisted" function declaration, which might make a difference depending on how/where the function is used.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • @bergi Question was about function style for **action creators** however, where none of this makes any difference. – timotgl Apr 07 '18 at 17:10
  • Action creators were only an example of context - was just curious generally! – anthonyhumphreys Apr 08 '18 at 14:44
  • @timotgl Yes, none of this makes a difference, that's why there is no difference in performance either. – Bergi Apr 08 '18 at 15:43
  • @timotgl However, aren't action creators typically `export`ed from ES6 modules? In that case, hoisting could make a difference. – Bergi Apr 08 '18 at 15:44