each method vs. for in

JavaScript performance comparison

Test case created by Oliver Caldwell

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  Object.applyEach = function(target, callback) {
      var key = null;
      
      for(key in target) {
          if(target.hasOwnProperty(key)) {
              callback.apply(target, [callback]);
          }
      }
  };
  
  Object.callEach = function(target, callback) {
      var key = null;
      
      for(key in target) {
          if(target.hasOwnProperty(key)) {
              callback.call(target, callback);
          }
      }
  };
  
  var testObject = {},
      copyTo = null;
  
  for(var i = 0; i < 100; i += 1) {
      testObject[i] = i * new Date().getTime();
  }

};
</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
applyEach
Object.applyEach(testObject, function(i) {
    copyTo = i;
});
pending…
callEach
Object.callEach(testObject, function(i) {
    copyTo = i;
});
pending…
Normal loop
var key = null;

for(key in testObject) {
    if(testObject.hasOwnProperty(key)) {
        copyTo = 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.

0 Comments