Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
deepcopy
(version: 0)
Comparing performance of:
deepClone vs deepClone_simple vs deepCloneOrAssign
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
obj = { sum: { calc: "wSum", //sum, wSum, ave, wAve, hwAve forGroup: true, }, formatting: { dataType: "number", //[int, number, pct1, pct100, folioCurrency, assetCurrency] }, tableDisplay: { visibility: "user", //always, default, user editable: true, columnTemplate_name: "number", showSumInGroup: true, showSum: true, formatting: { dataType: "number", //[int, number, pct1, pct100, folioCurrency, assetCurrency] decimals: 1, //preFix: "", //postFix: "", }, //this triggers on click of the button, as well as on double-tap and long-tap on a touch device headerButtonMenu: [ {text: "Remove Column", method: "removeField"} ], cellContextMenu: [ {text: "Clear", method: "clearCell"}, ], headerContextMenu: [ {text: "Clear All", method: "clearColumn"}, ] } } deepClone_simple = function(o) { let isArray = Array.isArray; let objectKeys = Object.keys; return copy(o); function copy(val) { if (!val) return val; if (isArray(val)) { let arr = []; let length = val.length; for (let i = 0; i < length; i++) { val2 = val[i] if (isArray(val2)) { let arr2 = []; let length2 = val.length; for (let i = 0; i < length; i++) arr.push(copy(val[i])) return arr; } else if (typeof val === 'object') { let keys = objectKeys(val); let newObject = {}; for (let i = keys.length - 1; i > -1; i--) { let key = keys[i]; newObject[key] = copy(val[key]); } return newObject; } return val; arr.push(copy(val[i])) } return arr; } else if (typeof val === 'object') { let keys = objectKeys(val); let newObject = {}; for (let i = keys.length - 1; i > -1; i--) { let key = keys[i]; newObject[key] = copy(val[key]); } return newObject; } return val; } } deepClone = function(o){ // if not array or object or is null return self let deepClone = this.deepClone; if (typeof o !== 'object'||o === null) return o; let newO, i; // handle case: array if (o instanceof Array) { let l, i; newO = []; for (i = 0, l = o.length; i < l; i++) newO[i] = deepClone(o[i]); return newO; } // handle case: object newO = {}; for (i in o) if (o.hasOwnProperty(i)) newO[i] = deepClone(o[i]); return newO; } deepCloneOrAssign = function(o, dest){ // only deep assigns objects and arrays, NOT dates, maps, sets etc. let deepCloneOrAssign = this.deepCloneOrAssign; if (typeof o !== 'object'||o === null) return o; let newO, i; // handle case: array if (o instanceof Array) { dest = dest ?? []; let l, i; for (i = 0, l = o.length; i < l; i++) dest[i] = deepCloneOrAssign(o[i], dest[i]); return newO; } // handle case: object dest = dest ?? {}; for (i in o) if (o.hasOwnProperty(i)) dest[i] = deepCloneOrAssign(o[i], dest[i]); return dest; }
Tests:
deepClone
return deepClone(obj)
deepClone_simple
return deepClone_simple(obj)
deepCloneOrAssign
return deepCloneOrAssign(obj)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
deepClone
deepClone_simple
deepCloneOrAssign
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark definition and explain what's being tested. **Benchmark Definition** The benchmark consists of three test cases, each representing a different implementation of the `deepClone` function: 1. `deepClone`: This is a simple clone function that returns the original object if it's not an object or null. 2. `deepClone_simple`: This is another simple clone function that uses a loop to iterate through the object's properties and recursively clones them. 3. `deepCloneOrAssign`: This is a more complex clone function that assigns the cloned value to a destination object, handling arrays and objects separately. **What's being tested** The benchmark tests the performance of each implementation on different inputs (i.e., various JavaScript objects). The test cases are: * `deepClone`: Tests the simple clone function with an object as input. * `deepClone_simple`: Tests the simple clone function with an object as input and uses a different variable name (`obj2`). * `deepCloneOrAssign`: Tests the more complex clone function with an object as input. **Options compared** The benchmark compares three different approaches to cloning objects: 1. **Simple clone**: The original implementation, which simply returns the original object if it's not an object or null. 2. **Simple recursive clone**: The `deepClone_simple` implementation, which uses a loop to iterate through the object's properties and recursively clones them. 3. **Complex clone with assignment**: The `deepCloneOrAssign` implementation, which assigns the cloned value to a destination object, handling arrays and objects separately. **Performance** The benchmark measures the performance of each implementation in terms of executions per second (ExecutionsPerSecond). **Latest benchmark result** The latest benchmark result shows that: * `deepCloneOrAssign` has the highest execution rate (~163.797 executions/second), indicating it's likely the fastest and most efficient implementation. * `deepClone_simple` has a lower execution rate (~105.194 executions/second) compared to `deepCloneOrAssign`, but still outperforms the simple clone function. * The simple clone function has the lowest execution rate (~146.022 executions/second), indicating it's likely the slowest and least efficient implementation. In summary, the benchmark shows that `deepCloneOrAssign` is the fastest and most efficient implementation for cloning objects, followed closely by `deepClone_simple`, and then the simple clone function.
Related benchmarks:
case native vs custom
Lodash cloneDeep VS spread operator VS Object.assign
1A theming lodash merge vs spread
JavaScript spread operator vs Object.assign vs swc performance vs structuredClone
JavaScript spread operator vs Object.assign vs swc performance vs structuredClone vs object.keys
Comments
Confirm delete:
Do you really want to delete benchmark?