Script Preparation code:
x
 
var obj = {
  description: 'Creates a deep copy of source, which should be an object or an array.',
  myNumber: 123456789,
  myBoolean: true,
  jayson: {
    stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....',
    parse: 'JSON.parse() method parses a JSON string...'
  }
};
var myCopy = null;
Tests:
  • JSON.parse(JSON.stringify())

     
    JSON.parse(JSON.stringify(obj));
  • structuredClone()

     
    structuredClone(obj);
  • fast_deep_clone()

     
    const fast_deep_clone = (obj) => {
        function cloneArray(a, fn) {
            var keys = Object.keys(a);
            var a2 = new Array(keys.length)
            for (const key of keys) {
              var k = keys[key];
              var cur = a[k];
              if (typeof cur !== 'object' || cur === null) {
                a2[k] = cur;
              } else {
                a2[k] = fn(cur);
              }
            }
            return a2;
        }
        if (typeof obj !== 'object' || obj === null) return obj;
        if (obj instanceof Date) return new Date(obj);
        if (Array.isArray(obj)) return cloneArray(obj, fast_deep_clone);
        if (obj.toString() !== '[object Object]') {
            return obj.toString();
        }
        let value, key;
        const keys = Object.keys(obj);
        const outObject = {};
      
        for (key of keys) {
          if (key) {
            value = obj[key];
            outObject[key] = fast_deep_clone(value);
          }
        }
      
        return outObject;
    };
    fast_deep_clone(obj);
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    JSON.parse(JSON.stringify())
    structuredClone()
    fast_deep_clone()

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: one year ago)
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36
Chrome 112 on Mac OS X 10.15.7
View result in a separate tab
Test name Executions per second
JSON.parse(JSON.stringify()) 719232.6 Ops/sec
structuredClone() 614901.4 Ops/sec
fast_deep_clone() 1704037.6 Ops/sec