Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object cloning with Lodash clone vs ES6 object spread vs ES6 Object.assign vs Json
(version: 0)
Comparing performance of:
Lodash clone vs ES6 spread vs ES6 Object.assign vs Json
Created:
7 years ago
by:
Registered User
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 o = { a: { b: 1, c: 2, d: 3, j: { k: [1,2,3], l: [4,5,6] }, }, e: [1,2,3,4,5,6], f: 1, g: { h: 1, } }
Tests:
Lodash clone
const a = _.clone(o)
ES6 spread
const a = { ...o }
ES6 Object.assign
const a = Object.assign({}, o)
Json
const a = JSON.parse(JSON.stringify(o))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Lodash clone
ES6 spread
ES6 Object.assign
Json
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash clone
4848486.5 Ops/sec
ES6 spread
67636848.0 Ops/sec
ES6 Object.assign
51091688.0 Ops/sec
Json
1274177.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided benchmark. **Benchmark Definition** The benchmark measures the performance of different approaches for creating a deep clone of an object. The test case uses a sample object `o` with nested properties and arrays, which is then cloned using four different methods: 1. **Lodash clone**: Uses the `_clone()` function from the Lodash library to create a deep copy of the object. 2. **ES6 spread**: Uses the syntax `{ ...o }` to create a shallow copy of the object (not suitable for deep cloning). 3. **ES6 Object.assign**: Uses the `Object.assign()` method with an empty object as the target to create a shallow copy of the object (not suitable for deep cloning). 4. **Json**: Uses `JSON.parse(JSON.stringify(o))` to create a deep clone of the object. **Library and Syntax** * Lodash is a popular JavaScript utility library that provides various functions for tasks like array manipulation, string formatting, and more. In this case, it's used for creating a deep clone of the object. * ES6 spread (`{ ...o }`) is a shorthand syntax for creating a new object with all the properties of `o`. While it can be useful for shallow cloning, it doesn't work well for deep cloning due to issues like circular references. **Pros and Cons** Here's a brief summary of each approach: 1. **Lodash clone**: Pros: * Works well for deep cloning, including handling circular references. * Provides a more reliable way to create a deep copy of an object. Cons: * Requires the Lodash library to be included in the test environment. 2. **ES6 spread**: Cons: * Only works for shallow cloning (not suitable for deep cloning). * May not handle circular references correctly. Pros: * Easy to use and concise syntax. * No additional library required. 3. **ES6 Object.assign**: Cons: * Only works for shallow cloning (not suitable for deep cloning). * May not handle circular references correctly. Pros: * Simple syntax and no additional library required. 4. **Json**: Pros: * Works well for deep cloning, including handling circular references. Cons: * Requires JSON parsing and stringification, which can be slower than Lodash's clone function. **Other Considerations** When choosing a method for deep cloning an object, consider the following factors: * Performance: Lodash's clone function is likely to be faster than using JSON parsing and stringification. * Code readability: Using Lodash's clone function or ES6 spread can make your code more readable by explicitly stating the intent of creating a new object. * Library dependencies: If you need to include an additional library (Lodash), consider whether it's worth the extra weight. **Alternatives** If you don't want to use Lodash, you could explore other libraries like: * `lodash-deep-clone` or `deep-clone`: Specialized deep cloning libraries that are smaller and faster than Lodash. * `JSON.parse(JSON.stringify())` with a custom implementation for handling circular references. Keep in mind that these alternatives might have different trade-offs in terms of performance, code readability, and library dependencies.
Related benchmarks:
Object cloning with Lodash clone vs cloneDeep vs merge vs ES6 object spread vs ES6 Object.assign
Object cloning with Lodash clone vs cloneDeep vs merge vs ES6 object spread vs ES6 Object.assign vs JSON.parse(JSON.stringify())
Object cloning with Lodash cloneDeep vs ES6 object spread vs JSON.stringify
Object cloning with Lodash clone vs cloneDeep vs merge vs ES6 object spread vs ES6 Object.assign VS JSON.parse
Comments
Confirm delete:
Do you really want to delete benchmark?