perf for in loop vs pre enum keys for loop

JavaScript performance comparison

Revision 8 of this test case created by uupaa

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function createTestData(count) {
      var rv = {}, i = 0;
  
      for (; i < count; ++i) {
          rv[i] = i;
      }
      return rv;
  }
  
  function Object_keys(source) {
      var rv = [], key, ri = 0;
  
      for (key in source) {
          if (source.hasOwnProperty(key)) {
              rv[ri++] = key;
          }
      }
      return rv;
  }
  
  function Date_now() {
      return +new Date();
  }
  
  window.testData = createTestData(200);
  
  Date.now || (Date.now = Date_now);
  Object.keys || (Object.keys = Object_keys);
  Object.prototype.oreore1 = "oreore1";
  Object.prototype.oreore2 = "oreore2";
  Object.prototype.oreore3 = "oreore3";
  Object.prototype.oreore4 = "oreore4";
  

};
</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
for in + hasOwnProperty loop
function for_in_hasOwnProperty(hash) {
    var rv = [], key;

    for (key in hash) {
        if (hash.hasOwnProperty(key)) {
            rv.push(key);
        }
    }
    return rv;
}
for_in_hasOwnProperty(testData)
pending…
uupaa looper
function uupaa_looper(hash) {
    var rv = [], ary = Object.keys(hash), i = 0, iz = ary.length;

    for (; i < iz; ++i) {
        rv.push(ary[i]); // key
    }
    return rv;
}
uupaa_looper(testData)
pending…
for in loop
function for_in(hash) {
    var rv = [], key;

    for (key in hash) {
        //if (hash.hasOwnProperty(key)) {
            rv.push(key);
        //}
    }
    return rv;
}
for_in(testData)
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.

0 Comments