for vs every vs _.every

JavaScript performance comparison

Revision 4 of this test case created

Info

Compares the cost of verifying if all elements of an array, after a transform, are truthy.

Preparation code

<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.1/underscore-min.js"></script>
<script type="text/javascript"></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

Test runner

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

Java applet disabled.

Testing in unknown unknown
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. Here’s a list of current revisions for this page:

0 comments

Add a comment