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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With