Object.defineProperty vs. Object.defineProperties

JavaScript performance comparison

Test case created by Talita Pagani

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var o = function() {
  	this._a = null;
  	this._b = null;
  	this._c = null;
  };
  
  Object.defineProperty(o, "a", {get : function(){ return this._a; },
                                 set : function(newValue){ this._a = newValue; },
                                 enumerable : true,
                                 configurable : true});
                                 
  Object.defineProperty(o, "b", {get : function(){ return this._b; },
                                 set : function(newValue){ this._b = newValue; },
                                 enumerable : true,
                                 configurable : true});
                                 
  Object.defineProperty(o, "c", {get : function(){ return this._c; },
                                 set : function(newValue){ this._c = newValue; },
                                 enumerable : true,
                                 configurable : true});
                                 
  
  var o2 = function() {
  	this._a = null;
  	this._b = null;
  	this._c = null;
  	
  	Object.defineProperty(this, "a", {get : function(){ return this._a; },
                                 set : function(newValue){ this._a = newValue; },
                                 enumerable : true,
                                 configurable : true});
                                 
  	Object.defineProperty(this, "b", {get : function(){ return this._b; },
  	                               set : function(newValue){ this._b = newValue; },
  	                               enumerable : true,
  	                               configurable : true});
  	                               
  	Object.defineProperty(this, "c", {get : function(){ return this._c; },
  	                               set : function(newValue){ this._c = newValue; },
  	                               enumerable : true,
  	                               configurable : true});
  };                             
  
  // Definindo várias propriedades com defineProperties
  var o3 = function() {
  	this._a = null;
  	this._b = null;
  	this._c = null;
  };
  
  Object.defineProperties(o3, {
  	"a": {
  		get : function(){ return this._a; },
  		set : function(newValue){ this._a = newValue; },
  		enumerable : true,
  		configurable : true
  	},
  	"b": {
  		get : function(){ return this._b; },
  		set : function(newValue){ this._b = newValue; },
  		enumerable : true,
  		configurable : true
  	},
  	"c": {
  		get : function(){ return this._c; },
  		set : function(newValue){ this._c = newValue; },
  		enumerable : true,
  		configurable : true
  	}
  });
  
  var o4 = function() {
  	this._a = null;
  	this._b = null;
  	this._c = null;
  
  	Object.defineProperties(this, {
  		"a": {
  			get : function(){ return this._a; },
  			set : function(newValue){ this._a = newValue; },
  			enumerable : true,
  			configurable : true
  		},
  		"b": {
  			get : function(){ return this._b; },
  			set : function(newValue){ this._b = newValue; },
  			enumerable : true,
  			configurable : true
  		},
  		"c": {
  			get : function(){ return this._c; },
  			set : function(newValue){ this._c = newValue; },
  			enumerable : true,
  			configurable : true
  		}
  	});
  
  };

};
</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
defineProperty outside function
var obj1 = new o();
obj1.a = 'property A';
obj1.b = 'property B';
obj1.c = 'property C';
alert(obj1.a);
alert(obj1.b);
alert(obj1.c);
pending…
defineProperty inside function
var obj2 = new o2();
obj2.a = 'property A';
obj2.b = 'property B';
obj2.c = 'property C';
alert(obj2.a);
alert(obj2.b);
alert(obj2.c);
pending…
defineProperties outside function
var obj3 = new o3();
obj3.a = 'property A';
obj3.b = 'property B';
obj3.c = 'property C';
alert(obj3.a);
alert(obj3.b);
alert(obj3.c);
pending…
defineProperties inside function
var obj4 = new o4();
obj4.a = 'property A';
obj4.b = 'property B';
obj4.c = 'property C';
alert(obj4.a);
alert(obj4.b);
alert(obj4.c);
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