underscore-index-vs-increment-count

JavaScript performance comparison

Test case created by Joshua Cody

Info

What's faster: using Underscore methods to get the integer index of an object you're iterating over or iterating a counter?

Preparation code

<script src="https://raw.github.com/documentcloud/underscore/ba3e31b53ef5752b40cfb2d71f594536fefbe916/underscore-min.js"></script>
<script>
Benchmark.prototype.setup = function() {
    var i = 20,
        hash = {};
   
    while( i-- ){
      var key = Math.random().toString().substring(7),
          subKey = Math.random().toString().substring(7);
   
      hash[key] = {};
      hash[key][subKey] = {};
      hash[key][subKey] = Math.random().toString().substring(7);
    }
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Underscore
_.each(hash, function(a, b, c){
  var ind = _.indexOf(_.keys(c), b);
});
pending…
Native
var counter = 0;

_.each(hash, function(a, b, c){
  counter++;
});
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