Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test clone deep
(version: 0)
Comparing performance of:
Copy Array x vs Copy Array lodash vs Copy Array json
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:
function x(clone, obj) { for(var i in obj) clone[i] = (typeof obj[i] == "object" && obj[i] != null) ? x(obj[i].constructor(), obj[i]) : obj[i]; return clone; } var copyArray = []; var copyObject = { id: 1, name: "Hello", x: true, i: { y: false, a: { b: { nested: true, last: "world" }, foo: "bar" } } }; for(var i = 0; i < 10000; i++) { copyArray.push(_.cloneDeep(copyObject)); }
Tests:
Copy Array x
var y = x([], copyArray);
Copy Array lodash
var y = _.cloneDeep(copyArray)
Copy Array json
var y = JSON.parse(JSON.stringify(copyArray))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Copy Array x
Copy Array lodash
Copy Array json
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.1:latest
, generated one year ago):
Let's dive into the benchmark tests. **Benchmark Description** The benchmark tests the performance of three different methods for cloning an array in JavaScript: `x()`, `_cloneDeep()` from Lodash, and `JSON.parse(JSON.stringify())`. **Test Cases** There are three individual test cases: 1. **Copy Array x**: This test uses a custom function `x()` to clone the `copyArray`. The function recursively iterates over each element in the array, creating a new copy if the element is an object. 2. **Copy Array lodash**: This test uses the `_cloneDeep()` method from Lodash to clone the `copyArray`. 3. **Copy Array json**: This test uses the `JSON.parse(JSON.stringify())` approach to clone the `copyArray`. **Library Used** The test case "Copy Array lodash" uses the Lodash library, which is a popular JavaScript utility library providing functions for common tasks like data manipulation and array operations. **JavaScript Feature or Syntax** None of the tests use any special JavaScript features or syntax. They rely on standard JavaScript constructs like loops, conditional statements, and function calls. **Options Compared** The three test cases compare the performance of different methods for cloning an array: 1. **x()**: A custom recursive function that clones objects by iterating over their properties. 2. **_.cloneDeep()**: A method from Lodash that recursively clones arrays and objects using a more efficient algorithm. 3. **JSON.parse(JSON.stringify())**: An approach that uses the `JSON.parse()` and `JSON.stringify()` methods to create a new JSON string representation of the array, which is then parsed back into an object. **Pros/Cons** Here are some pros and cons for each method: 1. **x()**: * Pros: Simple implementation, easy to understand. * Cons: May be slower than optimized methods, requires manual recursion handling. 2. **_.cloneDeep()**: * Pros: Optimized algorithm, efficient performance, widely used in the community. * Cons: Requires Lodash library inclusion, might be slower for small arrays. 3. **JSON.parse(JSON.stringify())**: * Pros: Easy to implement, no external library required. * Cons: May be slower than optimized methods, creates unnecessary JSON string intermediates. **Other Alternatives** There are other ways to clone an array in JavaScript, such as using the `Array.prototype.map()` method or the `Object.assign()` method. However, these approaches might not provide the same level of performance and efficiency as the three test cases described above. In conclusion, this benchmark tests the performance of different methods for cloning an array in JavaScript: a custom recursive function, Lodash's `_cloneDeep()`, and `JSON.parse(JSON.stringify())`. The results show that each method has its strengths and weaknesses, with Lodash's approach being the most efficient overall.
Related benchmarks:
Lodash cloneDeep vs for loop vs JSON parse vs recursive clone deep vs recursive reduce clone deep
ES6 vs Lodash object copying
Comparing deep cloning methods (array of objects): Lodash <> Custom clone func <> JSON.parse <> structuredClone
is lodash cloneDeep the BEST object deep cloner ? what about native structuredClone function ?
Lodash clone deep object array vs string array
Comments
Confirm delete:
Do you really want to delete benchmark?