Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Clone tests
(version: 0)
Comparing performance of:
deep clone vs Spread vs JSON
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var biggishObject = { name: "Sam Wiseman III", address: '1234 Orchid Drive, /n Minneapolis, MN', favoriteColors:['blue','green', 'silver'], homeTown: "Seattle, WA", pets: true, petNames: ['Cabbage', 'Nancy','Tom Collins'], carDriven: { make: 'Toyota', model: 'GR 86', year: 2024, color: 'blue' }, dob: '1972-07-30' } copy = null;
Tests:
deep clone
copy = _.cloneDeep(biggishObject)
Spread
copy = { ...biggishObject }
JSON
copy = JSON.parse(JSON.stringify(biggishObject))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
deep clone
Spread
JSON
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 YaBrowser/24.7.0.0 Safari/537.36
Browser/OS:
Yandex Browser 24 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
deep clone
589606.2 Ops/sec
Spread
5505852.0 Ops/sec
JSON
595031.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, along with the pros and cons of each approach. **Benchmark Overview** The benchmark compares three methods for creating a deep copy of a complex JavaScript object: `_.cloneDeep` from Lodash, Spread Operator (`{ ... }`), and JSON parsing (`JSON.parse(JSON.stringify())`). The benchmark creates a large object (`biggishObject`) with nested properties and then measures the execution time of each cloning method. **Cloning Methods** 1. **Lodash's _.cloneDeep**: This method uses recursion to create a deep copy of the object, including all its nested properties. * Pros: + Handles complex objects with circular references. + Fast and efficient for large objects. * Cons: + Requires Lodash library (included in benchmark via HTML script tag). + May be slower than other methods due to recursion overhead. 2. **Spread Operator (`{ ... }`)**: This method uses the spread operator to create a new object with all properties from the original object, including nested objects. * Pros: + Fast and efficient for large objects. + Does not require any external libraries. * Cons: + May not work correctly with circular references or deeply nested objects. + Can be slower than other methods due to property iteration overhead. 3. **JSON Parsing (`JSON.parse(JSON.stringify())`)**: This method uses JSON parsing to create a deep copy of the object, including all its nested properties. * Pros: + Handles complex objects with circular references. + Fast and efficient for large objects. * Cons: + Requires additional JSON parsing step, which can be slower than direct property iteration. **Library: Lodash** Lodash is a popular JavaScript utility library that provides various functions for tasks like array manipulation, string processing, and object cloning. In this benchmark, the `_.cloneDeep` method is used to create a deep copy of the complex object. **Special JS Feature/Syntax: None** There are no special JavaScript features or syntaxes being tested in this benchmark. **Other Alternatives** Some alternative methods for creating deep copies of objects include: * Using `Object.assign()` and then recursively iterating over properties * Using a library like `fast-deep-copy` (which is not included in the benchmark) * Implementing a custom recursive cloning function Keep in mind that each method has its trade-offs, and the best approach depends on specific use cases and performance requirements.
Related benchmarks:
Lodash benchmark (Anton)
Lodash cloneDeep vs JSON Clone with Array
Shallow clone array of objects
cloneDeep vs JSON.*
Lodash lodashoooon 2
Comments
Confirm delete:
Do you really want to delete benchmark?