Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

f.call(null,x) vs f(x) speed

Guys I would like a theoretical answer for this question.

I would like to know if calling f.call(null,x) is any possibly slower than calling f(x) ?

like image 261
Pacerier Avatar asked Jul 17 '26 06:07

Pacerier


2 Answers

As this test shows, executing the function directly, wins.

 function foo(x) {
  for (var i = 0; i < 100; i++);
 }

 // Tests
 foo('Bob');
 foo.call(null, 'Fish');
 foo.call(window, 'Cowboy!');
like image 91
Matt Avatar answered Jul 18 '26 18:07

Matt


Yes, it will be very marginally slower because of the addition property lookup (finding member call on function f). It is very much a micro-optimization and not something that should put you off using call() when it's necessary.

On the other hand, I've noticed that functions created with bind() are slower than their unbound counterparts and also slower than using call(), in most browsers. Just in case you were thinking that may be a way around any performance deficit.

Note that someone has already set up some tests for this over at http://jsperf.com.

like image 28
Andy E Avatar answered Jul 18 '26 18:07

Andy E



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!