HTML Preparation code:
AخA
 
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
2
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
Tests:
  • spread ...

    x
     
    const arr1 = [ true, 1, "true", [], {}, function () {} ];
    // Deep copies: true, 1, "true"
    // Shallow copies: [], {}, function () {}
    const arr2 = [ ...arr1 ];
  • slice()

     
    const arr1 = [ true, 1, "true", [], {}, function () {} ];
    // Deep copies: true, 1, "true"
    // Shallow copies: [], {}, function () {}
    const arr2 = arr1.slice();
  • splice(0)

     
    const arr1 = [ true, 1, "true", [], {}, function () {} ];
    // Deep copies: true, 1, "true"
    // Shallow copies: [], {}, function () {}
    const arr2 = arr1.splice(0);
  • concat()

     
    const arr1 = [ true, 1, "true", [], {}, function () {} ];
    // Deep copies: true, 1, "true"
    // Shallow copies: [], {}, function () {}
    const arr2 = arr1.concat();
  • JSON.parse(JSON.stringify())

     
    const arr1 = [ true, 1, "true", [], {}, function () {} ];
    // Deep copies: true, 1, "true", [], {}
    // Shallow copies: function () {}
    const arr2 = JSON.parse(JSON.stringify(arr1));
  • Custom function

     
    function copy(aObject) {
      // Prevent undefined objects
      // if (!aObject) return aObject;
      let bObject = Array.isArray(aObject) ? [] : {};
      let value;
      for (const key in aObject) {
        // Prevent self-references to parent object
        // if (Object.is(aObject[key], aObject)) continue;
        value = aObject[key];
        bObject[key] = (typeof value === "object") ? copy(value) : value;
      }
      return bObject;
    }
    const arr1 = [ true, 1, "true", [], {}, function () {} ];
    // Deep copies: true, 1, "true", [], {}, function () {}
    const arr2 = copy(arr1);
  • Lodash.cloneDeep()

     
    const arr1 = [ true, 1, "true", [], {}, function () {} ];
    // Deep copies: true, 1, "true", [], {}, function () {}
    const arr2 = _.cloneDeep(arr1);
  • jQuery.extend(true)

     
    const arr1 = [ true, 1, "true", [], {}, function () {} ];
    // Deep copies: true, 1, "true", [], {}, function () {}
    const arr2 = jQuery.extend(true, [], arr1);
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    spread ...
    slice()
    splice(0)
    concat()
    JSON.parse(JSON.stringify())
    Custom function
    Lodash.cloneDeep()
    jQuery.extend(true)

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 22 hours ago)
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0
Chrome 133 on Windows
View result in a separate tab
Test name Executions per second
spread ... 31990060.0 Ops/sec
slice() 35041720.0 Ops/sec
splice(0) 21033552.0 Ops/sec
concat() 25795440.0 Ops/sec
JSON.parse(JSON.stringify()) 2745774.5 Ops/sec
Custom function 2197669.8 Ops/sec
Lodash.cloneDeep() 976146.6 Ops/sec
jQuery.extend(true) 1083708.8 Ops/sec