0

well i'm building some javascript code and im just curious about benchmark of passing function in argument vs direct access

I got following functions

testIt(function(){
    alert('Hi test');
});

function testIt(func){
    func();
};

function testIt2(){
    alert('Hi test');
};

And now how about testIt vs testIt2? Would testIt be slower?

  • 3
    Two function invocations take probably more time than one function invocation. However, the performance difference will most likely be negligible. – Felix Kling Oct 22 '14 at 16:14
  • testIt and testIt2 are not at all the same function, even if they will perform the same in this example. Apples and Oranges. Since there is no 'not relevant/not interesting' tag, i suggest closing as too broad. – GameAlchemist Oct 22 '14 at 16:18
  • Why call a `testIt` function at all? Just directly do only `alert('hi test')`, it will be much faster. – Bergi Oct 22 '14 at 16:22
  • testIt2 function should be faster as testit has overhead of putting another function on call stack.But difference should be negligible. – WebServer Oct 22 '14 at 16:23

1 Answers1

0

I test it on jspref and here is my result:

Anonymous call have the same speed with direct call. But creating function on fly work ~80% slower.

So if you want to run some code in cycle — first define function and than pass it anonymously. It give enough readability and speed.

Zav
  • 671
  • 3
  • 8