each method vs. for in

Benchmark created by Oliver Caldwell on


Description

A test of function wrappers compared to the long hand.

Setup

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();
    }

Test runner

Ready to run.

Testing in
TestOps/sec
applyEach
Object.applyEach(testObject, function(i) {
    copyTo = i;
});
ready
callEach
Object.callEach(testObject, function(i) {
    copyTo = i;
});
ready
Normal loop
var key = null;

for(key in testObject) {
    if(testObject.hasOwnProperty(key)) {
        copyTo = i;
    }
}
ready

Revisions

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

  • Revision 1: published by Oliver Caldwell on