Count elements in object

JavaScript performance comparison

Test case created by fserb

Preparation code

<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.2/underscore-min.js"></script>
<script>
Benchmark.prototype.setup = function() {
    var o = {};
   
    for (var i = 0; i < 1000; ++i) {
       o[i] = true;
    }
};
</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
Object.keys
var l = Object.keys(o).length;
pending…
loop
var l = 0;
for (k in o) {
  if (o.hasOwnProperty(k)) l++;
}
 
pending…
loop 2
var l = 0;
for (k in o) l++;
 
pending…
underscore
var l = _.size(o);
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