最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - JavaScript performance: Call vs Apply - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question asked Oct 10, 2015 at 14:12 vitaly-tvitaly-t 26k17 gold badges127 silver badges150 bronze badges 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. – Jared Smith 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. – vitaly-t 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. – JJJ 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! – vitaly-t Commented Oct 10, 2015 at 14:27
  • Well the difference is 0.00000003 seconds per operation on my puter... but sure, if you have a million calls in a loop you'll save 30 milliseconds. – JJJ Commented Oct 10, 2015 at 15:37
 |  Show 4 more ments

3 Answers 3

Reset to default 3

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

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.

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 ω
发布评论

评论列表(0)

  1. 暂无评论