Object.create vs. clone

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;
    
    if (Object.create) {
      create = function (o) {
        return Object.create(o);
      };
    } else {
      (function () {
        var noop = function () {};
        create = 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;
    };

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

Revisions

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