Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Shallow spread vs Deep Clone
(version: 0)
Simple test showing, why Deep Clone should be use wisely.
Comparing performance of:
Shallow spread vs Deep Clone
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var MyObject = { 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:
Shallow spread
myCopy = {...MyObject};
Deep Clone
myCopy = JSON.parse(JSON.stringify(MyObject));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Shallow spread
Deep Clone
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0
Browser/OS:
Chrome 129 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Shallow spread
6965627.0 Ops/sec
Deep Clone
799397.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested. **Benchmark Definition** The benchmark defines two test cases: Shallow spread and Deep Clone. The purpose of this benchmark is to compare the performance of creating a shallow copy vs a deep clone of an object using JavaScript. **Shallow Spread (Test Case 1)** In the Shallow spread test case, the script preparation code creates an object `MyObject` with nested properties, including another object `jayson`. The benchmark definition code then assigns a new variable `myCopy` and uses the spread operator (`...`) to create a shallow copy of `MyObject`, assigning it to `myCopy`. **Deep Clone (Test Case 2)** In the Deep Clone test case, the script preparation code creates an object `MyObject` with nested properties. The benchmark definition code then assigns a new variable `myCopy` and uses the `JSON.parse(JSON.stringify(MyObject))` method to create a deep clone of `MyObject`, assigning it to `myCopy`. **Comparison** The comparison between Shallow spread and Deep Clone is aimed at determining which approach is faster and more efficient. **Pros and Cons** * **Shallow Spread:** + Pros: - Faster execution time, since only the top-level properties are copied. - More memory-efficient, since only the top-level properties are cloned. + Cons: - May lead to unexpected behavior if the original object is modified elsewhere in the codebase. - May not be suitable for objects with complex nested structures. * **Deep Clone:** + Pros: - Ensures that the cloned object is a fully independent copy, without any references to the original object. - Suitable for objects with complex nested structures or if the original object needs to remain unchanged. + Cons: - Slower execution time, since all properties and nested objects need to be copied. - More memory-intensive, since more data is transferred during cloning. **Library Usage** In this benchmark, the `JSON.parse()` and `JSON.stringify()` methods are used to create a deep clone of the object. These methods are part of the JavaScript standard library and provide a convenient way to convert between JSON strings and JavaScript objects. **Special JS Feature or Syntax** There is no special JavaScript feature or syntax used in this benchmark, other than the use of the spread operator (`...`) for shallow copying. The `JSON.parse()` and `JSON.stringify()` methods are standard features of the JavaScript language. **Other Alternatives** If you need to create a deep clone of an object, you can also use libraries like Lodash or underscore.js, which provide more advanced cloning functionality. Alternatively, you can write your own recursive function to manually copy nested objects. Some examples of alternative approaches include: * Using `Object.assign()` and `Array.prototype.slice()` * Using a library like Lodash's `cloneDeep()` method * Writing a custom recursive function to clone nested objects However, for most use cases, the built-in `JSON.parse()` and `JSON.stringify()` methods or the spread operator (`...`) are sufficient and efficient ways to create copies of JavaScript objects.
Related benchmarks:
JavaScript spread operator vs Object.assign performance for cloning
Deep Clone Object: JSON.parse vs Object.assign
Lodash cloneDeep vs Lodash clone vs Array.slice() vs. Object.assign()
Object copy JSON vs Object.assign
object.assign vs spread operator for shallow copying large objects 2
Comments
Confirm delete:
Do you really want to delete benchmark?