new vs Object.create (including polyfill)

JavaScript performance comparison

Revision 3 of this test case created by gero3

Info

testing of new vs native object.create when supported or polyfill where needed

Preparation code

<script>
  if (typeof Object.create !== 'function') {
   Object.create = function(o, props) {
    function F() {}
    F.prototype = o;
 
    if (typeof(props) === "object") {
     for (prop in props) {
      if (props.hasOwnProperty((prop))) {
       F[prop] = props[prop];
      }
     }
    }
    return new F();
   };
  }
  var GeometryCount  = 0;
  var Foo = function() {
        this.id = GeometryCount ++;

        this.vertices = [];
        this.colors = []; // one-to-one vertex colors, used in ParticleSystem, Line and Ribbon

        this.materials = [];

        this.faces = [];

        this.faceUvs = [[]];
        this.faceVertexUvs = [[]];

        this.morphTargets = [];
        this.morphColors = [];
        this.morphNormals = [];

        this.skinWeights = [];
        this.skinIndices = [];

        this.boundingBox = null;
        this.boundingSphere = null;

        this.hasTangents = false;

        this.dynamic = false; // unless set to true the *Arrays will be deleted once sent to a buffer.
};
  Foo.prototype.bar = "bar";
  Foo.prototype.baz = "baz";
  Foo.prototype.bang = "bang";

var Object3DCount  = 0;
var Object3D = function () {

        this.id = Object3DCount ++;

        this.name = '';

        this.parent = undefined;
        this.children = [];

        this.up = new Vector3();

        this.position = new Vector3();
        this.rotation = new Vector3();
        this.eulerOrder = 'XYZ';
        this.scale = new Vector3();

        this.doubleSided = false;
        this.flipSided = false;

        this.renderDepth = null;

        this.rotationAutoUpdate = true;

        this.matrix = new Vector3();
        this.matrixWorld = new Vector3();
        this.matrixRotationWorld = new Vector3();

        this.matrixAutoUpdate = true;
        this.matrixWorldNeedsUpdate = true;

        this.quaternion = new Vector3();
        this.useQuaternion = false;

        this.boundRadius = 0.0;
        this.boundRadiusScale = 1.0;

        this.visible = true;

        this.castShadow = false;
        this.receiveShadow = false;

        this.frustumCulled = true;

        this._vector = new Vector3();

}

 Object3D.prototype.bar = "bar";
  Object3D.prototype.baz = "baz";
  Object3D.prototype.bang = "bang";

var Vector3 = function(x,y,z){
this.elements = []

}
</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
simple constuctor
var obj = new Foo();
pending…
Object.create
var obj = Object.create(Foo.prototype)
pending…
complex constructor
var obj = new Object3D();
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