Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React performance: bind vs anonymous function

I would like to know if there is a performance difference between using bind and an anonymous function in React components.

Concretely, is one of the following more performant than another?

const MyComponent = ({ myHandler }) => {
  ...
  return (
    <a onClick={myHandler.bind(this, foo, bar)} ...>
      hello world
    </a>
  );
}

const MyComponent = ({ myHandler }) => {
  ...
  return (
    <a
      onClick={() => {
        myHandler(this, foo, bar)
      }
      ...
    >
      hello world
    </a>
  );
}

This question is different from the possible duplicate because the answer to the possible duplicate question focuses on the memory footprint.

like image 624
Sung Cho Avatar asked Mar 23 '17 00:03

Sung Cho


People also ask

Are anonymous functions faster?

Anonymous objects are faster than named objects. But calling more functions is more expensive, and to a degree which eclipses any savings you might get from using anonymous functions. Each function called adds to the call stack, which introduces a small but non-trivial amount of overhead.

Why not use anonymous functions in React?

Because we are passing anonymous functions, React will always re-render since it receives a new anonymous function as a prop which it is unable to compare to the previous anonymous function (since they are both anonymous). On the other hand, passing in a reference to the method like onClick={this.

What are the advantages of anonymous functions?

The advantage of an anonymous function is that it does not have to be stored in a separate file. This can greatly simplify programs, as often calculations are very simple and the use of anonymous functions reduces the number of code files necessary for a program.

Why are anonymous functions frequently used with event handlers?

More generally, the point of using anonymous functions is that they do not require a name, because they are "event handlers" bound to a specific event on a specific object. In this case, the object is the entire Document Object Model, and the anonymous function executes when the entire DOM has loaded.


1 Answers

First off, the way you are setting your question is a bit erraneous:

Your first example <a onClick={myHandler.bind(this, foo, bar)} ...> creates a new function on each render that is bound to the context of the component and always has foo and bar as the first two arguments.

Your second example <a onClick={() => myHandler(this, foo, bar)} ...> instead calls myHandler with 3 arguments, this, foo and bar, where this represents the component context.

On the theoretical level you're discussing about two concepts: is binding this to the function more expensive than creating a new anonymous bound function and calling the function there? I would say that both approaches are very similar from the performance point of view: you're creating a new function (func.bind docs) in both cases.

Any performance differences would be so small that you likely will never develop such a performance-critical piece of software where choosing between () => {} and bind would be an issue.

If you wish to enhance performance here, opt to use a class and bind the click handler to the context in the constructor. That way you do not need to create a new function every time you render.

Hope this helps

like image 62
jakee Avatar answered Nov 08 '22 07:11

jakee