Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash deepclone vs native
(version: 0)
Comparing performance of:
json stringify vs lodash deep clone vs Object.assign
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js'></script>
Tests:
json stringify
var obj = {a: "hello", c: "test", po: 33, arr: [1, 2, 3, 4], anotherObj: {a: 33, str: "whazzup"}}; var obj2 = JSON.parse(JSON.stringify(obj));
lodash deep clone
var obj = {a: "hello", c: "test", po: 33, arr: [1, 2, 3, 4], anotherObj: {a: 33, str: "whazzup"}}; var obj2 = _.cloneDeep(obj, true);
Object.assign
var obj = {a: "hello", c: "test", po: 33, arr: [1, 2, 3, 4], anotherObj: {a: 33, str: "whazzup"}}; var obj2 = Object.assign({}, obj);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
json stringify
lodash deep clone
Object.assign
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 is being tested. **Benchmark Definition:** The benchmark definition describes three test cases that compare different approaches to creating a deep clone of an object: 1. `json stringify` - This approach uses the built-in `JSON.stringify()` function in combination with `JSON.parse()` to create a deep clone. 2. `lodash deep clone` - This approach uses the `cloneDeep()` function from the Lodash library to create a deep clone. 3. `Object.assign()` **Options Compared:** The benchmark compares these three approaches: * `json stringify`: Uses `JSON.stringify()` and `JSON.parse()` to create a deep clone. * `lodash deep clone`: Uses the `cloneDeep()` function from the Lodash library to create a deep clone. * `Object.assign()`: Uses the `Object.assign()` method to create a shallow clone, which is not suitable for deep cloning. **Pros and Cons:** Here are some pros and cons of each approach: * `json stringify`: + Pros: Simple, widely available, and fast. + Cons: Can be slow for large objects, and the resulting string representation may not be desirable. * `lodash deep clone`: + Pros: Provides a robust and flexible way to create deep clones, with options to customize the cloning process. + Cons: Requires including an additional library (Lodash). * `Object.assign()`: + Pros: Simple and widely available, but only suitable for shallow cloning. + Cons: Not suitable for deep cloning, and can be slow due to the need to recursively clone properties. **Library: Lodash** The `lodash deep clone` approach uses the `cloneDeep()` function from the Lodash library. Lodash is a popular utility library that provides a wide range of functions for common tasks, such as array manipulation, object cloning, and more. The `cloneDeep()` function is specifically designed to create deep clones of objects, making it a convenient choice for this benchmark. **Special JS Feature/Syntax: None** There are no special JavaScript features or syntax used in the benchmark that would require additional explanation. **Other Alternatives** Other alternatives for creating deep clones include: * Using a library like Immer or Immutable.js, which provide more advanced object manipulation capabilities. * Implementing a custom deep cloning function using recursion and property iteration. * Using a library like TypeScript's built-in `Pick` and `Clone` types, which provide more modern and type-safe ways to create deep clones. However, these alternatives are not part of the standard JavaScript ecosystem and may require additional setup or learning curves. The benchmark uses Lodash as a convenient and widely available solution for this specific task.
Related benchmarks:
isEmpty vs. vanilla
Lodash.js vs Native - empty
lodash vs deepmerge vs deepmerge-ts
Comparing performance of native .length and Lodash _.isEmpty v2
My lodash vs deepmerge vs deepmerge-ts
Comments
Confirm delete:
Do you really want to delete benchmark?