for foreach

JavaScript performance comparison

Revision 2 of this test case created by OSM Buildings

Preparation code

<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/1.0.0-rc.3/lodash.min.js"></script>

<script>
  var media = [];
  var types = [123, 666, 999, 'test', 'hello', 'bar', 0];
  for (var i = 0; i < 100; i++) {
    media[i] = {
      name: i,
      type: types
    };
  }
  media[50].type = types.concat('foo');
</script>
<script>
Benchmark.prototype.setup = function() {
    var type = 'foo';
    var result;
};
</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, len = media.length; i < len; i++) {
  var a = media[i];
//  if (~media[i].type.indexOf(type)) {
//    result = media[i];
//    break;
//  }
}
pending…
lodash find
result = _.find(media, function(mediaItem) {
  var a = mediaItem;
//  return ~_.indexOf(mediaItem.type, type);
});
pending…
lodash forEach
_.forEach (media, function (mediaItem) {
  var a = mediaItem;
//  if (!result && ~mediaItem.type.indexOf(type)) {
//    result = mediaItem;
//  }
});
pending…
while
var i = media.length;
while (--i) {
  var a = media[i];
}
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:

1 comment

crispy commented :

Testcase 4 suffers off-by-one error! need

while(i--) {
    //…
}

Your's won't get index 0 and does funny things if media.length should ever be 0.

Add a comment