Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash cloneDeep vs structuredClone on many
(version: 0)
https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
Comparing performance of:
Lodash cloneDeep vs Native structuredClone
Created:
2 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 myObjects = []; 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...' } }; [...Array(10000)].forEach((_, i) => { myObjects.push(structuredClone(MyObject)); }); var myCopy = null;
Tests:
Lodash cloneDeep
myCopy = _.cloneDeep(myObjects);
Native structuredClone
myCopy = structuredClone(myObjects);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
Native structuredClone
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):
**Overview of the Benchmark** The provided JSON represents a JavaScript microbenchmark that compares the performance of two approaches to create a deep copy of an object: Lodash's `cloneDeep` function and the native `structuredClone` API. **Library Used: Lodash** Lodash is a popular utility library for JavaScript that provides a wide range of functions for tasks such as array manipulation, object creation, and functional programming. The `cloneDeep` function is specifically designed to create deep copies of objects, which means it recursively clones all nested properties and arrays within the object. **Library Used: structuredClone** The `structuredClone` API is a new feature introduced in ECMAScript 2020 that provides a way to create deep copies of objects without using explicit loops or recursive functions. It works by serializing the object into a format called "structured clone" and then deserializing it back into an object. **Comparison of Approaches** The two approaches being compared are: 1. **Lodash `cloneDeep`**: This approach uses Lodash's `cloneDeep` function to create a deep copy of the `myObjects` array, which contains 10,000 copies of the same `MyObject`. The resulting object is stored in the `myCopy` variable. 2. **Native `structuredClone`**: This approach uses the native `structuredClone` API to create a deep copy of the `myObjects` array. **Pros and Cons** **Lodash `cloneDeep`** Pros: * Widely supported by most browsers and Node.js environments * Easy to use and understand, with a simple syntax * Provides additional features like array cloning and object merging Cons: * Adds extra overhead due to the library's dependencies and execution time * May not be as efficient as native implementations for very large datasets **Native `structuredClone`** Pros: * Optimized for performance and memory efficiency * Only relies on the native API, making it more lightweight * Supports advanced features like object serialization and deserialization Cons: * Currently supported by only recent browsers (e.g., Chrome 114) and Node.js environments * Requires a good understanding of the `structuredClone` API and its limitations **Other Considerations** * **Async vs. Sync**: Both approaches can be used with asynchronous functions, but Lodash's `cloneDeep` is designed for synchronous use cases. * **Error Handling**: Neither approach provides explicit error handling; if something goes wrong during cloning, it will result in an incorrect or incomplete copy. **Alternative Approaches** If you need to create deep copies of objects and want a native implementation, you can consider using: 1. **JSON.parse(JSON.stringify(object))**: This method is simple but not recommended due to its limitations (e.g., it doesn't support circular references). 2. **Array.prototype.slice() + Object.assign()**: This approach involves creating a shallow copy of the array and then recursively cloning each item. 3. **lodash's `clone` function**: If you don't need the advanced features of `cloneDeep`, Lodash's simpler `clone` function might be sufficient. Keep in mind that these alternatives may have different performance characteristics, especially for large datasets or complex objects.
Related benchmarks:
Lodash cloneDeep vs structuredClone vs recursiveDeepCopy vs JSON clone 10kb json
Object Clone Lodash vs structuredClone
Lodash cloneDeep vs structuredClone vs JSON.stringify (small object)
Lodash cloneDeep vs structuredClone vs JSON-JSON
Lodash cloneDeep vs structuredClone vs JSON Parse (100 000 objects)
Comments
Confirm delete:
Do you really want to delete benchmark?