1

Is there a performance benefit in switching from func.apply(obj, params) to func.call(obj) when params is an empty array or null?

I mean, is calling func.call(obj) any faster than calling func.apply(obj, null)?

I'm mostly interested in performance under NodeJS 4.x.

This is for an algorithm that has to make a lot of such calls.

asked Oct 10, 2015 at 14:12
9
  • 2
    I realize that such an algorithm may make a lot of those calls, but its usually premature optimization to worry about such a trivial detail that bloats your code before profiling. Commented Oct 10, 2015 at 14:16
  • This is not being helpful. I'm asking for some real numbers for this sort of optimization, not to tell me how to do my own measurements or approach writing my algorithm. Commented Oct 10, 2015 at 14:21
  • The thing is, as with almost all micro-optimization, that the actual performance of a minuscule operation like this is dependent on the algorithm that uses it. You might well find that in isolated cases one is faster than the other, but in practice it's the other way around. There's no way to know for sure without trying it in the real use case. Commented Oct 10, 2015 at 14:24
  • In my case I have to rely heavily on this kind of calls. Anyhow, the answer by Magu is excellent, and the numbers speak for themselves! Commented Oct 10, 2015 at 14:27
  • Well the difference is 0.00000003 seconds per operation on my computer... but sure, if you have a million calls in a loop you'll save 30 milliseconds. Commented Oct 10, 2015 at 15:37

3 Answers 3

3

On this page there is a comparison. https://jsperf.com/call-apply-segu Call was faster on my machine.

answered Oct 10, 2015 at 14:21
Sign up to request clarification or add additional context in comments.

Comments

2

Basically, they will do the same steps:

Function.prototype.apply (thisArg, argArray)

  1. If IsCallable(func) is false, then throw a TypeError exception.
  2. If argArray is null or undefined, then
    1. Return the result of calling the [[Call]] internal method of func, providing thisArg as the this value and an empty list of arguments.

Function.prototype.call (thisArg [ , arg1 [ , arg2, ... ] ] )

  1. If IsCallable(func) is false, then throw a TypeError exception.
  2. Let argList be an empty List.
  3. If this method was called with more than one argument then in left to right order starting with arg1 append each argument as the last element of argList
  4. Return the result of calling the [[Call]] internal method of func, providing thisArg as the this value and argList as the list of arguments.

So the difference, if any, should be implementation dependent, and negligible.

answered Oct 10, 2015 at 14:23

1 Comment

Thank you for the detailed explanation, but the answer by Magu gives the exact numbers, which is perfect for what I was looking for.
1

Ha, interesting: it looks like apply is slower than call. 8-)

 ~/tmp ω cat test.js 
function work(a, b, c) {
 // do some work
}
var a = [1, 2, 3];
for (var j = 0; j < 4; j++) {
 console.time('apply-ing');
 for (var i = 0; i < 1000000; i++) {
 work.apply(this, a);
 }
 console.timeEnd('apply-ing');
 console.time('call-ing');
 for (var i = 0; i < 1000000; i++) {
 work.call(this, 1, 2, 3);
 }
 console.timeEnd('call-ing');
}
 ~/tmp ω node test.js
apply-ing: 42ms
call-ing: 5ms
apply-ing: 40ms
call-ing: 5ms
apply-ing: 42ms
call-ing: 5ms
apply-ing: 39ms
call-ing: 6ms
 ~/tmp ω node --version
v4.1.2
 ~/tmp ω
answered Oct 10, 2015 at 14:22

2 Comments

According to the link published by Magu, apply is a lot slower than call.
it is an incorrect implementation. To justify everything better to use call like this work.call(this, ...a). And apply will win. Just only because apply here uses argument config as an argument. It's more useful for realworld

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.