lundi 28 septembre 2020

Does the complexity of a closure affect the performance cost of creating it?

Suppose I have the following React hook:

export const useSomeState = (someProps: {}) => {
  return React.useState(() => {
    // Some really complex function with lots of code
  })
}

Because we are using the function handler to React.useState, the state should only be initted once.. thats fine...

If this hook is used as part of a larger component that is re-rendered frequently does this hurt performance? Or do I need to use React.useCallback or something?

What is the performance cost of JS creating and re-creating a closure (on each re-render) that it will never run?

And does the complexity of the closure (number of variables captured and put on the heap) affect the performance cost?




Aucun commentaire:

Enregistrer un commentaire