Run details:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36
Chrome 119
Mac OS X 10.15.7
Desktop
one year ago
Test name Executions per second
A 2406405.2 Ops/sec
B 3221296.2 Ops/sec
C 4528368.5 Ops/sec
D 4542309.0 Ops/sec
Script Preparation code:
x
 
// Declaring our Animal object
var Animal = function () {
    this.name = 'unknown';
    this.getName = function () {
        return this.name;
    }
    return this;
};
// Declaring our Dog object
var Dog = function () {
    // A private variable here        
    var private = 42;
    // overriding the name
    this.name = "Bello";
    // Implementing ".bark()"
    this.bark = function () {
        return 'MEOW';
    }  
    return this;
};
// Dog extends animal
Dog.prototype = new Animal();
// Creating an instance of Dog.
var obj = new Dog();
function iterateA() {
  var ret = "";
    
  for (var prop in obj) {
    if( obj.hasOwnProperty( prop ) ) {
      ret += obj[prop];
    } 
  }
    
  return ret;
}
function iterateB() {
  var ret = "";
    
  Object.keys(obj).forEach(function (prop) {
    ret += obj[prop];
  });
  return ret;
}
function iterateC() {
   var ret = "";
   for (var i = 0, keys = Object.keys(obj); i < keys.length; i++) {
       ret += obj[i];
   }
   return ret;
}
function iterateD() {
   var ret = "";
   for (var keys = Object.keys(obj), len = keys.length, i = 0; i < len; i++) {
       ret += obj[i];
   }
   return ret;
}
Tests:
  • A

     
    var tmp = iterateA();
  • B

     
    var tmp = iterateB();
  • C

     
    var tmp = iterateC();
  • D

     
    var tmp = iterateD();