object-property-iteration

JavaScript performance comparison

Test case created by baetheus

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function work(value) {
    // WORK IT
  }
  
  function createTestObj(count) {
    var obj = {}
    while (count--) {
      obj["key" + count] = "test";
    }
    return obj;
  }
  
  function objKeysWhile(obj) {
    var keys = Object.keys(obj);
    var i = keys.length;
    while (i--) {
      work(obj[keys[i]]);
    }
  }
  
  function objKeysFor(obj) {
    var keys = Object.keys(obj);
    var len = keys.length;
    var i;
    for (i = 0; i < len; i++) {
      work(obj[keys[i]]);
    }
  }
  
  function forInHas(obj) {
    for (key in obj) {
      if (obj.hasOwnProperty(key)) {
        work(obj[key]);
      }
    }
  }
  
  function forIn(obj) {
    for (key in obj) {
      work(obj[key]);
    }
  }
  
  function objKeysForEach(obj) {
    Object.keys(obj).forEach(work);
  }
  
  var small = createTestObj(50);
  var large = createTestObj(10000);

};
</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
Small, Object.keys, While
objKeysWhile(small);
pending…
Small, Object.keys, For
objKeysFor(small);
pending…
Small, Object.keys, forEach
objKeysForEach(small);
pending…
Small, For In, hasOwn
forInHas(small);
pending…
Small, For In
forIn(small);
pending…
Large, Object.keys, While
objKeysWhile(large);
pending…
Large, Object.keys, For
objKeysFor(large);
pending…
Large, Object.keys, forEach
objKeysForEach(large);
pending…
Large, For In, hasOwn
forInHas(large);
pending…
Large, For In
forIn(large);
pending…

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

Compare results of other browsers

0 Comments