Object.keys vs for in for values

JavaScript performance comparison

Revision 11 of this test case created

Preparation code

<script>

</script>
<script>
Benchmark.prototype.setup = function() {
    var keys = Object.keys;
    var hasOwnProperty = Object.prototype.hasOwnProperty;
   
    var getObj1 = function() { return {
    'prop1': 'value1',
    'prop2': 'value2',
    'prop3': 'value3',
    'prop4': 'value4',
    'prop5': 'value5',
    'prop6': 'value6',
    'prop7': 'value7',
    'prop8': 'value8',
    'prop9': 'value9',
    'prop10': 'value10'
    }};
   
    var getObj2 = function() { return {
    'prop2': 'value22',
    'prop4': 'value44',
    'prop7': 'value77',
    'prop8': 'value88',
    'prop10': 'value1010'
    }};
};
</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 obj1 = new getObj1();
var obj2 = new getObj2();
var props = keys(obj2);
var index = -1;
var length = keys.length;

while (++index < length) {
   var prop = props[index];
   obj1[prop] = obj2[prop];
}
pending…
for-in w/ hasOwnProperty
var obj1 = new getObj1();
var obj2 = new getObj2();

for (var prop in obj2) {
  obj1[prop] = obj2[prop];
}
pending…
unrolled loop
var obj1 = new getObj1();
var obj2 = new getObj2();

obj1.prop1 = (obj2.prop1 !== undefined) ? obj2.prop1 : obj1.prop1;
obj1.prop2 = (obj2.prop2 !== undefined) ? obj2.prop2 : obj1.prop2;
obj1.prop3 = (obj2.prop3 !== undefined) ? obj2.prop3 : obj1.prop3;
obj1.prop4 = (obj2.prop4 !== undefined) ? obj2.prop4 : obj1.prop4;
obj1.prop5 = (obj2.prop5 !== undefined) ? obj2.prop5 : obj1.prop5;
obj1.prop6 = (obj2.prop6 !== undefined) ? obj2.prop6 : obj1.prop6;
obj1.prop7 = (obj2.prop7 !== undefined) ? obj2.prop7 : obj1.prop7;
obj1.prop8 = (obj2.prop8 !== undefined) ? obj2.prop8 : obj1.prop8;
obj1.prop9 = (obj2.prop9 !== undefined) ? obj2.prop9 : obj1.prop9;
obj1.prop10 = (obj2.prop10 !== undefined) ? obj2.prop10 : obj1.prop10;
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