Property copying comparison using cached keys variable (v2)

Revision 2 of this benchmark created by Gildas on


Description

Tests the performance of copying properties using for..in versus Object.keys when Object.keys is cached in a local variable.

Preparation HTML

<script>
  var myObj = {
   foo: "foo",
   bar: "bar",
   baz: "baz"
  };
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
for..in
var copyTo = {};
for (var key in myObj) {
 if (myObj.hasOwnProperty(key)) {
  copyTo[key] = myObj[key];
 }
}
ready
Object.keys
var copyTo = {};
var keys = Object.keys(myObj);
keys.forEach(function(value) {
 copyTo[value] = myObj[value];
});
ready
Object.keys with classic for
var copyTo = {};
var keys = Object.keys(myObj);
var i, value, length = keys.length;
for (i = 0; i < length; i++) {
 value = keys[i];
 copyTo[value] = myObj[value];
}
ready

Revisions

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