for vs every vs _.every

JavaScript performance comparison

Revision 4 of this test case created by

Preparation code

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.1/underscore-min.js"></script>
<script></script>
      
<script>
Benchmark.prototype.setup = function() {
  var testArray = [];
  for (var x = 0; x < 1000; x++) {
    testArray[x] = x;
  }
  
  function testFunc(val, index, array) {
    var mul = val * val;
  }

};
</script>

Preparation code output

<script></script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
for
for (var i = 0; i < testArray.length; i++) {
  testFunc(testArray[i]);
}
pending…
every
var result = testArray.every(testFunc);
pending…
_.every
var result = _.every(testArray, testFunc); // aka, _.all
pending…
_.map + _.every
var result = _(testArray)
  .chain()
  .map(testFunc)
  .every(_.identity)
  .value();
pending…

Compare results of other browsers

Revisions

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

0 Comments