Function call cost (v9)

Revision 9 of this benchmark created by Dmitry Kulikov on


Description

Examines the cost of a simple function call.

Setup

function foo(a, b, c) {
      if (a === "a") { // a will never be "a"
        throw "Error";
      }
    }
    
    function loop(fn) {
        var index = 0;
        
        return function over(array) {
            if (index >= array.length) {
                return;
            }
            fn(array[index], index, array);
            index++;
            over(array);
        };
    }
    
    var array = [];
    (function() {
      var index = 10000;
      while (index) {
        array.push(index);
        --index;
      }
    })();
    
    var index, length;

Test runner

Ready to run.

Testing in
TestOps/sec
No call
for (index = 0; index < array.length; index += 1) {
  if (array[index] === "a") { // will never happen
    throw "Error";
  }
}
ready
With call
for (index = 0; index < array.length; index += 1) {
  foo(array[index], index, array);
}
ready
forEach (ES5 browsers only)
array.forEach(foo);
ready
forEach (inline function, ES5 browsers only)
array.forEach(function(a, b, c) {
  if (a === "a") { // will never happen
    throw "Error";
  }
});
ready
for(cached len)
for (index = 0, len = array.length; index < len; index += 1) {
  if (false) {
    throw "Error";
  }
}
ready
forEach
array.forEach(function() {
  if (false) { // will never happen
    throw "Error";
  }
});
ready
for(while)
var i = array.length;
while (i--) {
  if (false) {
    throw "Error";
  }
}
 
ready
loop No call
loop(function (item, index, array) {
  if (false) {
    throw "Error";
  }
})(array);
ready
loop call
loop(function (item, index, array) {
  foo(item, index, array);
})(array);
ready
for(opposite direction)
for (index = array.length - 1; index >= 0; index -= 1) {
    foo(array[index], index, array);
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by T.J. Crowder on
  • Revision 5: published on
  • Revision 9: published by Dmitry Kulikov on