<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>
<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>
const arr1 = [ true, 1, "true", [], {}, function () {} ];
// Deep copies: true, 1, "true"
// Shallow copies: [], {}, function () {}
const arr2 = [ arr1 ];
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.splice(0);
const arr1 = [ true, 1, "true", [], {}, function () {} ];
// Deep copies: true, 1, "true"
// Shallow copies: [], {}, function () {}
const arr2 = arr1.concat();
const arr1 = [ true, 1, "true", [], {}, function () {} ];
// Deep copies: true, 1, "true", [], {}
// Shallow copies: function () {}
const arr2 = JSON.parse(JSON.stringify(arr1));
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);
const arr1 = [ true, 1, "true", [], {}, function () {} ];
// Deep copies: true, 1, "true", [], {}, function () {}
const arr2 = _.cloneDeep(arr1);
const arr1 = [ true, 1, "true", [], {}, function () {} ];
// Deep copies: true, 1, "true", [], {}, function () {}
const arr2 = jQuery.extend(true, [], arr1);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
spread ... | |
slice() | |
splice(0) | |
concat() | |
JSON.parse(JSON.stringify()) | |
Custom function | |
Lodash.cloneDeep() | |
jQuery.extend(true) |
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 |
It seems like you've shared two benchmark results from a testing tool, likely JSTestdriver or similar.
From the results, it appears that:
slice()
function with an average of 62792596 executions per second.concat()
function is slightly slower, averaging around 52792284 executions per second.spread
function and splice(0)
have execution times in the hundreds of thousands, which seems slow compared to the others.JSON.parse(JSON.stringify())
function has a relatively slow average execution time of around 2489870.5 executions per second.The two benchmark tests that show up with actual numbers for Chrome 129 on Mac OS X 10.15.7 are:
slice()
: 62792596 executions/secondconcat()
: 52792284 executions/second