Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash (4.17.11) cloneDeep vs JSON Clone vs structuredClone
(version: 0)
Comparing performance of:
Lodash cloneDeep vs Json clone vs structuredClone
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js'></script>
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:
Lodash cloneDeep
myCopy = _.cloneDeep(MyObject);
Json clone
myCopy = JSON.parse(JSON.stringify(MyObject));
structuredClone
myCopy = structuredClone(MyObject);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
Json clone
structuredClone
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Browser/OS:
Chrome 146 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash cloneDeep
2166765.0 Ops/sec
Json clone
2210918.0 Ops/sec
structuredClone
962286.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation. **Overview** The provided JSON represents a benchmark test on MeasureThat.net, where three different approaches are compared to clone a JavaScript object: Lodash `cloneDeep`, JSON's `parse` and `stringify` methods, and the `structuredClone` function. The test creates a deep copy of an object using each approach and measures their performance. **Options Compared** The options being compared are: 1. **Lodash `cloneDeep`**: A library function that creates a deep copy of an object. 2. **JSON's `parse` and `stringify` methods**: Built-in JavaScript methods used to parse and convert JSON strings, respectively. 3. **`structuredClone` function**: A new function introduced in ECMAScript 2020, which creates a shallow copy of an object. **Pros and Cons** Here are some pros and cons for each approach: 1. **Lodash `cloneDeep`**: * Pros: Easy to use, efficient, and works well with complex objects. * Cons: Requires the Lodash library, which may be an overhead for small projects. 2. **JSON's `parse` and `stringify` methods**: * Pros: Built-in functions, easy to use, and does not require any additional libraries. * Cons: Not designed for deep object copying, can lead to unexpected results with circular references, and performance may be slower compared to dedicated cloning libraries like Lodash. 3. **`structuredClone` function**: * Pros: New and optimized function, designed specifically for creating shallow copies of objects, which may be more efficient than `cloneDeep`. * Cons: Still a relatively new feature, not widely supported by older browsers or environments. **Library: Lodash** Lodash is a popular JavaScript utility library that provides various functions for tasks like cloning, mapping, filtering, and more. In this benchmark, Lodash's `cloneDeep` function is used to create a deep copy of the input object. **Special JS Feature/Syntax** None mentioned in the provided JSON. However, it's worth noting that some newer JavaScript features, such as `Spread Operator` (e.g., `{...obj}`) or `Object.assign()` methods, might be used in certain cloning scenarios. **Other Alternatives** If you need to clone objects frequently, other alternatives to Lodash `cloneDeep` include: * **Array.prototype.slice()**: Creates a shallow copy of an array. * **JSON.parse(JSON.stringify(obj))`: Creates a deep copy of an object using JSON's `parse` and `stringify` methods (as shown in the benchmark). * **Object.assign({}, obj)**: Creates a shallow copy of an object using the spread operator. Keep in mind that each approach has its trade-offs, and the best choice depends on your specific use case and performance requirements.
Related benchmarks:
Lodash 2.2.0 cloneDeep vs JSON Clone w/ large nested object
Lodash cloneDeep vs structuredClone vs recursiveDeepCopy vs JSON clone with a more deep test
Lodash cloneDeep vs structuredClone vs Json.stringify
lodash cloneDeep vs. JSON.parse(JSON.stringify()) vs. structuredClone
Comments
Confirm delete:
Do you really want to delete benchmark?