clone

JavaScript performance comparison

Test case created by vpArth

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const orig = {a: 1, b: 2, c: {d: 3, e: 4}, f: [1, 2, 3, 4, 5]};
  function deepClone0(obj) {
    if (Array.isArray(obj)) {
      return obj.map(deepClone0);
    }
    if (typeof obj === 'object') {
      return Object.entries(obj).reduce((res, [k, v]) => (res[k] = deepClone0(v), res), {})
    }
    // Assume it's scalar
    return obj;
  }
  
  function deepClone1(obj) {
      const clObj = {};
      for(const i in obj) {
        if (obj[i] instanceof Object) {
          clObj[i] = deepClone1(obj[i]);
          continue;
        }
        clObj[i] = obj[i];
      }
      return clObj;
    }
  function deepClone2(obj) {
    return JSON.parse(JSON.stringify(obj));
  }
  function deepClone3(obj) {
    return new Notification('', {data: obj, silent: true}).data;
  }

};
</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
arrays aware
deepClone0(orig);
pending…
uDef
deepClone1(orig);
pending…
JSON
deepClone2(orig);
pending…
NotificationAPI
deepClone3(orig);
pending…

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

Compare results of other browsers

0 Comments