Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
deep clone JSON
(version: 0)
Comparing performance of:
json stringify vs lodash deep clone vs Object.assign
Created:
7 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js'></script>
Tests:
json stringify
var obj = {a: "hello", c: "test", po: 33, arr: [1, 2, 3, 4], anotherObj: {a: 33, str: "whazzup"}}; var obj2 = JSON.parse(JSON.stringify(obj));
lodash deep clone
var obj = {a: "hello", c: "test", po: 33, arr: [1, 2, 3, 4], anotherObj: {a: 33, str: "whazzup"}}; var obj2 = _.clone(obj, true);
Object.assign
var obj = {a: "hello", c: "test", po: 33, arr: [1, 2, 3, 4], anotherObj: {a: 33, str: "whazzup"}}; var obj2 = Object.assign({}, obj);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
json stringify
lodash deep clone
Object.assign
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 benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Context** The benchmark measures the performance of three different methods for creating a deep clone of a JavaScript object: 1. `JSON.parse(JSON.stringify(obj))` 2. `_._clone(obj, true)` (using Lodash library) 3. `Object.assign({}, obj)` **Options Compared** These are the three options being compared: * `JSON.parse(JSON.stringify(obj))`: This method uses JSON serialization to create a deep clone of the object. * `_._clone(obj, true)` (Lodash): This method is part of the Lodash library and provides a function for creating deep clones. The second argument (`true`) indicates that we want to perform a "deep" copy, which means it will recursively clone nested objects as well. * `Object.assign({}, obj)`: This method uses the spread operator (`{}`) to create a shallow copy of the object, and then recursively copies nested objects. **Pros and Cons** 1. **JSON.parse(JSON.stringify(obj))**: * Pros: Simple and widely supported; doesn't require any external libraries. * Cons: Can be slower than other methods due to the overhead of JSON serialization/deserialization. 2. **Lodash's _._clone(obj, true)**: * Pros: Efficient and optimized for deep cloning; provides a more comprehensive solution for cloning complex objects. * Cons: Requires including the Lodash library, which adds extra dependencies. 3. **Object.assign({}, obj)**: * Pros: Simple and widely supported; can be faster than other methods due to its use of spread operator. * Cons: Creates a shallow copy, so nested objects may not be cloned recursively. **Library and Special JS Features** In this benchmark, the Lodash library is used for its `_._clone` function. This library provides various utility functions for working with JavaScript objects, arrays, and other data structures. No special JS features or syntax are being tested in this benchmark. **Other Alternatives** If you were looking for alternative methods to create a deep clone of an object in JavaScript, some options could be: * Using the `lodash` library with `_._cloneDeep` (note: not used in this benchmark) * Using the `lodash` library with `_.extend({}, obj)` (not used in this benchmark) * Implementing your own recursive function to clone objects * Using a library like Immer for updating objects However, `Object.assign` is often a good default choice when you just need a shallow copy of an object. **Benchmark Preparation Code** The script preparation code includes the following: ```javascript <script src='https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js'></script> ``` This loads the Lodash library, which is used for the `_._clone` function in one of the benchmark cases. Please note that you may need to adjust this code based on your specific use case and requirements.
Related benchmarks:
Lodash deep clone vs JSON.stringfy
Lodash deep clone vs Spread Clone
Lodash cloneDeep vs JSON Clone with Array
Clone deep
Fair Lodash deep clone vs Spread Clone
Comments
Confirm delete:
Do you really want to delete benchmark?