Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash cloneDeep vs clone
(version: 0)
Comparing performance of:
Lodash cloneDeep vs Vanilla clone
Created:
7 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);
Vanilla clone
function clone(obj) { const clone = Object.create(Object.getPrototypeOf(obj)); Object.getOwnPropertyNames(obj).forEach(key => { Object.defineProperty(clone, key, Object.getOwnPropertyDescriptor(obj, key)); }); return clone; } myCopy = clone(MyObject);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
Vanilla clone
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:135.0) Gecko/20100101 Firefox/135.0
Browser/OS:
Firefox 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash cloneDeep
1105071.6 Ops/sec
Vanilla clone
4147275.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark measures the performance of two approaches to create a deep copy of an object: Lodash's `cloneDeep` function and a vanilla JavaScript implementation using `Object.create` and `Object.getOwnPropertyDescriptor`. **Library: Lodash** Lodash is a popular JavaScript utility library that provides various functions for tasks such as array manipulation, string processing, and object manipulation. In this benchmark, Lodash's `cloneDeep` function is used to create a deep copy of the input object. **Test Case 1: Lodash cloneDeep** The first test case uses the `cloneDeep` function from Lodash to create a deep copy of the input object `MyObject`. This approach relies on Lodash's implementation to handle the complexities of creating a deep copy, including handling nested objects and arrays. Pros: * Simplified implementation: The developer does not need to write custom logic for creating a deep copy. * Efficient: Lodash's implementation is optimized for performance and can handle complex object structures efficiently. Cons: * Dependency on external library: The test case relies on the presence of Lodash in the environment, which may not always be guaranteed. * Potential overhead: Depending on the version of Lodash used, there might be additional overhead due to its complexity and feature set. **Test Case 2: Vanilla clone** The second test case uses a vanilla JavaScript implementation to create a deep copy of the input object `MyObject`. This approach requires the developer to write custom logic for creating a deep copy, which can be more complex than using a library like Lodash. Pros: * No external dependencies: The test case does not rely on any external libraries, making it self-contained. * Customizable: The developer can tailor the implementation to specific requirements or optimize it for performance. Cons: * More complex implementation: Creating a deep copy manually requires more effort and expertise. * Potential errors: Without proper handling of edge cases, the vanilla implementation might introduce bugs or inefficiencies. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in these test cases. The focus is on comparing the performance of two different approaches to creating a deep copy. **Other Alternatives** If you're interested in exploring alternative approaches, here are some options: 1. **JSON.parse(JSON.stringify())**: This method can be used to create a deep copy of an object. However, it's not recommended for complex objects or arrays with cycles. 2. **Array.prototype.slice() + Object.assign()**: For creating a shallow copy of an object, you can use `Array.prototype.slice()` to clone the object's array properties and then use `Object.assign()` to merge the cloned array with the original object. 3. **For...of loops and Array.prototype.forEach()**: You can also create a deep copy by iterating over the object's properties using `for...of` loops and `Array.prototype.forEach()`, but this approach is more verbose and less efficient than the other methods. Keep in mind that each approach has its strengths and weaknesses, and the choice ultimately depends on your specific use case, performance requirements, and personal preference.
Related benchmarks:
Lodash cloneDeep vs clone vs spread
Lodash cloneDeep vs Lodash clone vs Array.slice() vs. Object.assign()
Lodash cloneDeep vs JSON Clone with Array
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()
Comments
Confirm delete:
Do you really want to delete benchmark?