Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash cloneDeep vs structuredClone, cloning of typedarrays
(version: 0)
https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
Comparing performance of:
Lodash cloneDeep vs Native 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.5/lodash.min.js'></script>
Script Preparation code:
var MyObject = { typed1: Int16Array.from(Array(500).fill(0).map((_, index) => (index % 20))), typed2: Int16Array.from(Array(1500).fill(0).map((_, index) => (index % 20))), typed3: Int16Array.from(Array(200).fill(0).map((_, index) => (index % 20))), typedRandom: Float32Array.from(Array(1500).fill(0).map(() => Math.random() * 200)), typedRandom2: Float32Array.from(Array(1500).fill(0).map(() => Math.random() * 200)), }; var myCopy = null;
Tests:
Lodash cloneDeep
myCopy = _.cloneDeep(MyObject);
Native structuredClone
myCopy = structuredClone(MyObject);
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):
The provided JSON represents a JavaScript benchmark test case on the MeasureThat.net website. The test compares two approaches to create a deep copy of an object: Lodash's `cloneDeep` function and the native `structuredClone` API. **Options compared:** 1. **Lodash cloneDeep**: This is a third-party library that provides a recursive function to create a deep copy of an object. It checks for cycles in the object graph, which can be problematic if not handled properly. 2. **Native structuredClone**: This is a new API introduced in ECMAScript 2020 (JavaScript 2021), designed to create deep copies of objects while preserving their structure and avoiding potential memory leaks. **Pros and Cons:** * **Lodash cloneDeep**: + Pros: - Well-maintained and widely adopted library. - Handles cycles in the object graph correctly. + Cons: - Adds additional dependency on Lodash, which may not be desirable for all projects. - Recursion-based approach can lead to stack overflow errors for very large objects. * **Native structuredClone**: + Pros: - Built-in API means no additional dependencies or potential security risks. - Optimized for performance and efficiency. + Cons: - Limited support (only introduced in ECMAScript 2020, which might not be supported by all browsers). - Requires careful handling of certain edge cases, such as cyclic references. **Library:** The `structuredClone` API is a built-in JavaScript function that creates a deep copy of an object while preserving its structure. It's designed to be more efficient and safer than Lodash's recursive cloning approach. The library in question is likely the Lodash.js implementation of `cloneDeep`. **Special JS feature or syntax:** There are no special features or syntaxes mentioned in this benchmark test case. **Benchmark preparation code:** The script preparation code creates an object `MyObject` with several properties, each containing a typed array (Int16Array and Float32Array). The `structuredClone` and Lodash's `cloneDeep` functions are then used to create copies of this object. **Other alternatives:** For creating deep copies of objects in JavaScript, other alternatives include: * **JSON.parse(JSON.stringify(obj))**: A simple but potentially slow approach that can lead to issues with cyclic references. * **Object.assign()**: Can also be used to create a shallow copy of an object, but it may not preserve the structure or handle cycles correctly. * **for...in** loop: An older approach that can be cumbersome and error-prone. Keep in mind that the choice of cloning method depends on the specific requirements and constraints of your project.
Related benchmarks:
Lodash cloneDeep vs Lodash clone vs Array.slice() vs. Object.assign()
Lodash cloneDeep vs. Lodash clone vs. Array.slice() vs. Array.slice(0) vs. Object.assign()
Lodash cloneDeep vs Lodash clone vs Array.splice() vs. Object.assign()
Lodash cloneDeep vs Lodash clone vs Array.splice() vs. Object.assign() vs Array.slice() vs Array.slice(0)
Object Clone Lodash vs structuredClone
Comments
Confirm delete:
Do you really want to delete benchmark?