Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Deep Copy of Array1
(version: 1)
Comparing performance of:
cloneDeep vs JSON vs DeepCopy
Created:
4 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
Script Preparation code:
var myArray = [{itemType:"text",itemId:"zy5mhESQ",text:"","sound":""},{itemType:"micromodule",itemId:"CczUjEMY",id:57191,scores:{rightAnswer:1000,partialAnswer:500,wrongAnswer:0}},{itemType:"text",itemId:"RSOqEJzl",text:"",sound:""}]; var myCopy = null; function recursiveDeepCopy(o) { var newO, i; if (typeof o !== 'object') { return o; } if (!o) { return o; } if ('[object Array]' === Object.prototype.toString.apply(o)) { newO = []; for (i = 0; i < o.length; i += 1) { newO[i] = recursiveDeepCopy(o[i]); } return newO; } newO = {}; for (i in o) { if (o.hasOwnProperty(i)) { newO[i] = recursiveDeepCopy(o[i]); } } return newO; }
Tests:
cloneDeep
myCopy = _.cloneDeep(myArray);
JSON
myCopy = JSON.parse(JSON.stringify(myArray));
DeepCopy
myCopy = recursiveDeepCopy(myArray);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
cloneDeep
JSON
DeepCopy
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):
I'll break down the provided benchmark and explain what's being tested, compared options, pros and cons, and other considerations. **Benchmark Definition JSON** The benchmark definition is a JSON object that contains information about the test case. In this case: * `Name`: The name of the benchmark, which is "Deep Copy of Array1". * `Description`: An empty string. * `Script Preparation Code`: A JavaScript code snippet that creates an array `myArray` with some sample data. * `Html Preparation Code`: A script tag that loads the Lodash library, which provides a `cloneDeep` function. **Individual Test Cases** The benchmark includes three test cases: 1. **"cloneDeep"`: * `Benchmark Definition`: `myCopy = _.cloneDeep(myArray);` * This test case is testing the performance of the `_.cloneDeep` function from Lodash. 2. **"JSON"`: * `Benchmark Definition`: `myCopy = JSON.parse(JSON.stringify(myArray));` * This test case is testing the performance of the built-in `JSON.parse` and `JSON.stringify` functions in JavaScript. 3. **"DeepCopy"`: * `Benchmark Definition`: `myCopy = recursiveDeepCopy(myArray);` * This test case is testing the performance of a custom implementation of deep copying, which is defined in the `Script Preparation Code`. **Comparison of Options** The three test cases compare different approaches to perform a deep copy of an array: 1. **Lodash's `_.cloneDeep`**: A widely-used library function that provides a fast and efficient way to create a deep copy of an object or array. 2. **Built-in `JSON.parse` and `JSON.stringify`**: These functions can be used to create a deep copy of an object, but they may not work as expected with all types of data. 3. **Custom implementation (`recursiveDeepCopy`)**: A bespoke solution that manually creates a new object or array and recursively copies the properties from the original. **Pros and Cons** Here are some pros and cons for each approach: 1. **Lodash's `_.cloneDeep`**: * Pros: Fast, efficient, and widely supported. * Cons: Dependent on the Lodash library, may not work with all types of data. 2. **Built-in `JSON.parse` and `JSON.stringify`**: * Pros: Fast, simple to implement, and works with most types of data. * Cons: May not work correctly with certain data types (e.g., functions, dates), can be slower than Lodash's implementation. 3. **Custom implementation (`recursiveDeepCopy`)**: * Pros: Customizable, can handle complex data structures, but may be slower and more error-prone than the other two options. **Other Considerations** * The benchmark uses a desktop browser (Chrome 90) to run the tests, which may not represent all possible use cases. * The `Html Preparation Code` loads the Lodash library asynchronously, which may affect the results of the test. * The benchmark does not account for other factors that could impact performance, such as caching or optimization. **Alternatives** If you're looking for alternative approaches to deep copying in JavaScript, consider: 1. **Object.assign()**: A simple and widely-supported way to copy objects, but may not work correctly with all types of data. 2. **Spread operator (`...`)**: A modern feature that allows you to create a shallow copy of an object or array using the spread operator. 3. **Third-party libraries**: Other libraries like `lodash-es`, `Immutable.js`, or `fast-deepcopy` offer alternative implementations of deep copying. Keep in mind that the choice of approach depends on your specific use case, performance requirements, and personal preference.
Related benchmarks:
deepcopy vs splice
Clone vs JSON.stringify+JSON.parse
For vs While and Shift vs Array.slice
Shallow Copy Array
Cloning an array
Comments
Confirm delete:
Do you really want to delete benchmark?