object properties init

JavaScript performance comparison

Revision 4 of this test case created by Alex Shvets

Preparation code

<script src="http://quadroid.github.io/clonejs/cdn/clone.min.js"></script>
<script>
Benchmark.prototype.setup = function() {
    var prototype = {
      name: "this is object to clone"
    };
   
    function cloneByProto(proto, values){
      values.__proto__ = proto;
      return values;
    }
   
    function cloneByObjCreateForIn(proto, values){
      var obj = Object.create(proto);
      for(var p in values)
        obj[p] = values[p];
      return obj;
    }
   
    function cloneByObjCreateDescriptors(proto, values){
      var descriptors = {};
      for(var p in values)
        descriptors[p] = {value: values[p], enumerable:true, configurable:true, writable:true};
      return Object.create(proto, descriptors);
    }
};
</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 literal
var objLiteral = {a:10,b:20,c:30};
 
pending…
object =
var o = new Object;
o.a=10; o.b=20; o.c=30;
pending…
new constructor
function Constructor(a,b,c){
  this.a=a; this.b=b; this.c=c;
}
Constructor.prototype = prototype;
var objByNew = new Constructor(10,20,30);
 
pending…
__proto__
var objByProto = cloneByProto(prototype, {a:10,b:20,c:30});
 
pending…
Object.create & for in
var objCreateByForIn = cloneByObjCreateForIn(prototype, {a:10,b:20,c:30});
pending…
Object.create & descriptors
var objCreateByDescriptors = cloneByObjCreateDescriptors(prototype, {a:10,b:20,c:30});
pending…
clonejs.org
var obj = clone(prototype, {a:10,b:20,c:30});
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