Object.create vs. clone (v2)

Revision 2 of this benchmark created on


Setup

var o = {
      a: 'a',
      b: 'b',
      c: 'c',
      d: 'd',
      e: {
        f: 'f',
        g: 'g',
        h: {
          i: 'i',
          j: 'j',
          k: 'k',
          l: 'l',
          m: {
            n: 'n',
            o: {
              p: 'p',
              q: 'q',
              r: 'r',
              s: 's',
              t: {
                u: 'u',
                v: 'v',
                w: 'w',
                x: 'x',
                y: 'y'
              }
            }
          }
        }
      },
      z: {}
    }, _o;
    
    var clone = function (o) {
      var _o = {}, p, value;
      for (p in o) {
        value = o[p];
        if (value === Object.prototype[p]) continue;
        _o[p] = value;
      }
      return _o;
    };
    
    var deepClone = function (o) {
      var _o = {}, p, value;
      for (p in o) {
        value = o[p];
        if (value === Object.prototype[p]) continue;
        if (value && typeof value === "object")
          _o[p] = deepClone(value);
        else
          _o[p] = value;
      }
      return _o;
    };
    
    var create;
    
    create = function (o) {
      return Object.create(o);
    };
    
    var fakeCreate = (function () {
      var noop = function () {};
      return function (o) {
        noop.prototype = o;
        return new noop();
      };
    })();
    
    var deepCreate = function (o) {
      var _o = Object.create(o), p, value;
      for (p in _o) {
        value = _o[p];
        if (value && typeof value === "object")
          _o[p] = deepCreate(value);
      }
      return _o;
    };
    
    var deepFakeCreate = function(o) {
      var _o = fakeCreate(o), p, value;
      for (p in _o) {
        value = _o[p];
        if (value && typeof value === "object")
          _o[p] = deepFakeCreate(value);
      }
      return _o;
    };

Test runner

Ready to run.

Testing in
TestOps/sec
Shallow clone
_o = clone(o);
ready
Shallow Object.create
_o = create(o);
ready
Deep clone
_o = deepClone(o);
ready
Deep create
_o = deepCreate(o);
ready
Shallow fake create
_o = fakeCreate(o);
ready
Deep fake create
_o = deepFakeCreate(o);
ready

Revisions

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