kojiro
kojiro

Reputation: 77137

Why are .call and .apply slower than a direct function call in JavaScript?

I'm curious about these jsperf results. They appear to demonstrate that a direct function call is substantially faster than the same function called with .call or .apply. (The difference between .call and .apply surprised me even more.) Could you please explain these results?

Update: Here is a jsperf that someone left that tests .apply without a second array instantiation.

Upvotes: 24

Views: 6972

Answers (1)

F-A
F-A

Reputation: 643

I guess the cause might depend on which interpreter your are running the code on, but it seems that normal functions calls are faster because the interpreter can use Inline Cache to access the properties.

You can have a look here for more information.

Upvotes: 7

Related Questions