Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
different clone methods
(version: 0)
Comparing performance of:
lodash vs structuredClone vs stringify
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.5/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
myCopy = _.cloneDeep(myObject);
structuredClone
myCopy = structuredClone(myObject);
stringify
myCopy = JSON.parse(JSON.stringify(myObject))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash
structuredClone
stringify
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):
Let's break down the provided JSON and explain what's being tested, compared, and considered in this JavaScript microbenchmark. **Benchmark Definition** The benchmark is comparing three different approaches to create a deep copy of an object: `lodash.cloneDeep()`, `structuredClone()`, and `JSON.parse(JSON.stringify())`. **Options Compared** 1. **Lodash`cloneDeep()`**: This method creates a deep clone of the source object, recursively copying all its properties. 2. **Structured Clone (`structuredClone`)**: This is a relatively new method introduced in JavaScript (ES2020) that allows creating a shallow copy of an object without serializing it to a string or JSON. It's designed for use cases where you need to preserve references between objects. 3. **`JSON.parse(JSON.stringify())`**: This approach uses the `JSON.stringify()` method to serialize the source object to a string, and then parses that string back into an object using `JSON.parse()`. While this works, it can be slower and less efficient than other methods. **Pros and Cons** * **Lodash`cloneDeep()`**: * Pros: Widely supported, easy to use, and has been extensively tested. * Cons: Can be slow for very large objects due to the recursive cloning process. * **Structured Clone (`structuredClone`)**: * Pros: Designed for performance and memory efficiency, can handle large objects quickly. * Cons: Requires JavaScript 2020 or later to use, and support might not be widespread yet. * **`JSON.parse(JSON.stringify())`**: * Pros: Simple and widely supported, but slower than other methods due to serialization and deserialization. **Library Used** In this benchmark, the `lodash` library is used for its `cloneDeep()` function. The `structuredClone()` method, on the other hand, is a built-in JavaScript function introduced in ES2020. **Special JS Feature/Syntax** None of the methods being tested use any special or advanced JavaScript features/syntax that would require specific knowledge to understand. **Alternative Approaches** Other approaches for creating deep copies of objects include: * Using the `Array.prototype.slice()` method with a shallow copy * Implementing a custom cloning function using recursion or iteration * Utilizing other libraries like `immer` or `deepcopy` Keep in mind that each approach has its pros and cons, and the choice ultimately depends on the specific use case, performance requirements, and compatibility considerations. The test results can be used to compare the execution times of these different approaches for creating deep copies of objects. This information can help developers make informed decisions about which method to use in their projects.
Related benchmarks:
Lodash cloneDeep vs JSON Clone with huge object
Lodash cloneDeep vs clone
Lodash cloneDeep vs JSON Clone with Array
Lodash cloneDeep vs JSON Clone vs Lodash clone
Lodash (4.17.11) cloneDeep vs JSON Clone vs structuredClone
Comments
Confirm delete:
Do you really want to delete benchmark?