Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
cloneDeep VS spread all around fixed
(version: 0)
Comparing performance of:
spread vs cloneDeep
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
window.sourceO = { foo: 42, bar: { list: [0, 1, 2, 3], text: 'hello' }, biz: { nest: { nest: { nest: { a: 42, b: 12 } } } } };
Tests:
spread
const copySpread = { ...window.sourceO, nest: {nest: {nest: {...window.sourceO.biz.nest.nest.nest, b: 42}}}};
cloneDeep
let copyCloneDeep = _.cloneDeep(window.sourceO); copyCloneDeep.biz.nest.nest.nest.b = 42;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
spread
cloneDeep
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):
Let's break down the provided benchmark definition and test cases to understand what is being tested. **Benchmark Definition:** The benchmark definition represents a JavaScript object that will be used as input for both test cases. It defines an object `window.sourceO` with several properties, including `foo`, `bar`, `biz`, and nested objects. ```json { "Name": "cloneDeep VS spread all around fixed", "Description": null, "Script Preparation Code": "...", // this code is executed before the benchmark starts "Html Preparation Code": null } ``` **Test Cases:** There are two test cases defined: 1. `spread`: ```json { "Benchmark Definition": "const copySpread = {\r\n...window.sourceO, \r\nnest: {nest: {nest: {...window.sourceO.biz.nest.nest.nest, b: 42}}}};", "Test Name": "spread" } ``` This test case uses the spread operator (`{...}`) to create a new object `copySpread` by copying all properties from `window.sourceO`, and then modifying the nested objects using the spread operator. 2. `cloneDeep`: ```json { "Benchmark Definition": "let copyCloneDeep = _.cloneDeep(window.sourceO);\r\ncopyCloneDeep.biz.nest.nest.nest.b = 42;", "Test Name": "cloneDeep" } ``` This test case uses the `_` library (likely Lodash) to clone the `window.sourceO` object using the `_.cloneDeep()` function, and then modifying the cloned object by assigning a new value to a specific property. **Options Compared:** The benchmark is comparing two approaches: 1. **Spread operator (`{...}`)**: This approach uses the spread operator to create a new object by copying all properties from the original object. 2. **CloneDeep (Lodash)**: This approach uses the `_` library's `_.cloneDeep()` function to clone the original object. **Pros and Cons of Each Approach:** 1. **Spread Operator (`{...}`)**: * Pros: + Fast and lightweight + Does not create a new object, but instead modifies the existing one (in this case, the `copySpread` variable is not used) * Cons: + May lead to unexpected behavior if not used carefully (e.g., modifying the original object) 2. **CloneDeep (Lodash)**: * Pros: + Provides a clear and explicit way to create a deep copy of an object + Can be more predictable than the spread operator in certain scenarios * Cons: + May be slower than the spread operator due to the overhead of creating a new object and cloning its properties **Library Used:** The `_` library is used for the `_.cloneDeep()` function. Lodash is a popular JavaScript utility library that provides a wide range of functions for working with objects, arrays, and other data structures. **Special JS Feature or Syntax:** None of the test cases use any special JavaScript features or syntax beyond what's standard in modern JavaScript. **Other Considerations:** * The benchmark is executed on a desktop platform (Windows) using Chrome 85. * The `ExecutionsPerSecond` value indicates that the benchmark was run multiple times, with the results averaged to produce the final values. As for alternatives, some possible options could include: * Using other deep cloning libraries, such as `deepClone()` from the `lodash.clone` module * Implementing a custom deep cloning function using recursion or iteration * Using a different approach altogether, such as modifying the original object in place (although this may lead to unexpected behavior)
Related benchmarks:
Lodash deep clone vs Spread Clone
cloneDeep VS spread all around
Lodash deeper clone vs Spread Clone
Fair Lodash deep clone vs Spread Clone
Comments
Confirm delete:
Do you really want to delete benchmark?