Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash cloneDeep vs structuredClone vs JSON clone s
(version: 0)
https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
Comparing performance of:
Lodash cloneDeep vs Native structuredClone vs JSON clone
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 cloneDeep
myCopy = _.cloneDeep(MyObject);
Native structuredClone
myCopy = structuredClone(MyObject);
JSON clone
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 cloneDeep
Native structuredClone
JSON clone
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 benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares three approaches for creating a deep copy of an object: 1. **Lodash cloneDeep**: Uses the `cloneDeep` function from the Lodash library to create a deep copy of the input object. 2. **Native structuredClone**: Utilizes the built-in `structuredClone` API (available in modern browsers) to create a deep copy of the input object. 3. **JSON clone**: Uses the `JSON.parse(JSON.stringify())` method to create a shallow copy of the input object. **Options Compared** The benchmark compares the performance of each approach across different browsers and devices. **Pros and Cons of Each Approach** 1. **Lodash cloneDeep** * Pros: Robust, widely supported, and easy to use. * Cons: Adds additional dependencies (Lodash library) and may introduce overhead due to its complexity. 2. **Native structuredClone** * Pros: Fast, lightweight, and only relies on the browser's API. * Cons: Requires modern browsers that support the `structuredClone` API, which might not be universally supported. 3. **JSON clone** * Pros: Simple, widely available, and doesn't require additional dependencies. * Cons: Creates a shallow copy (only copies property values, not recursive structures), which may lead to unexpected behavior in some cases. **Libraries Used** The benchmark uses the Lodash library for its `cloneDeep` function. The `structuredClone` API is built into modern browsers. **Special JS Features or Syntax** None of the tested approaches rely on special JavaScript features or syntax that would be unfamiliar to most software engineers. **Other Considerations** When choosing an approach, consider the trade-offs between performance, simplicity, and compatibility: * For simple copying tasks, JSON clone might be sufficient. * When working with complex objects or requiring a deep copy, Lodash cloneDeep or native structuredClone might be more suitable. * If targeting modern browsers, native structuredClone is likely the best choice. **Alternatives** Other approaches to creating a deep copy of an object include: 1. **Array.prototype.slice() and Object.assign()**: For arrays and simple objects. 2. **For...in loops and Object.keys()**: For more complex objects or when manually handling recursive structures. Keep in mind that these alternatives might not be as efficient or feature-rich as the Lodash cloneDeep approach, but they can still provide a good starting point for simpler copying tasks.
Related benchmarks:
Lodash cloneDeep vs structuredClone vs JSON.stringify (small object)
Lodash (4.17.11) cloneDeep vs JSON Clone vs structuredClone
Lodash cloneDeep vs structuredClone vs JSON-Clone
Lodash cloneDeep vs structuredClone vs JSON-JSON
Lodash cloneDeep vs structuredClone vs Json.stringify
Comments
Confirm delete:
Do you really want to delete benchmark?