Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
teste simple deep copy
(version: 0)
https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
Comparing performance of:
json parse vs simple copy
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: [true, false, undefined, true] }; var myCopy = null;
Tests:
json parse
myCopy = JSON.parse(JSON.stringify(MyObject));
simple copy
const removeDeepReference = (object) => { const newObject = {}; Object.keys(object).map((key) => { newObject[key] = object[key].slice(0, object[key].length); }); return newObject; }; myCopy = removeDeepReference(MyObject)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
json parse
simple copy
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 benchmark and explain what is being tested. **Benchmark Definition** The benchmark is testing two approaches to create a copy of an object: using `JSON.parse(JSON.stringify(MyObject))` and a custom function `removeDeepReference`. **Options Compared** The benchmark compares two options for creating a copy of an object: 1. **JSON.parse(JSON.stringify(MyObject))**: This method uses the JSON stringification and parsing algorithm to create a deep copy of the object. 2. **Custom Function: removeDeepReference**: This function manually iterates through each property of the original object, creates a new object with the same properties, and then copies the values from the original object to the new object. **Pros and Cons** 1. **JSON.parse(JSON.stringify(MyObject))**: * Pros: Simple, concise, and widely supported. * Cons: May not work as expected for complex objects (e.g., circular references) or objects with non-serializable values. 2. **Custom Function: removeDeepReference**: * Pros: Can handle more complex cases than `JSON.parse(JSON.stringify(MyObject))`, such as objects with circular references. * Cons: More verbose, requires manual implementation of the copy logic. **Library Used** The benchmark uses Lodash, a popular JavaScript utility library. Specifically, it uses the `lodash.min()` function to load the Lodash library. **JavaScript Feature/Syntax Used** This benchmark does not use any special JavaScript features or syntax that is specific to a particular browser or engine. **Other Considerations** When creating a copy of an object in JavaScript, it's essential to consider the following: * **Caching**: If the original object has cached properties (e.g., `const obj = { value: 10 }; obj.cache = true;`), they will be copied as well. * **Circular References**: If the original object contains circular references, the copy method may not work correctly. **Alternatives** Other approaches to creating a copy of an object include: 1. **Object.assign()**: This method can be used to create a shallow copy of an object. 2. **Array.prototype.slice()**: This method can be used to create a shallow copy of an array-like object. 3. **For...in loops**: These loops can be used to manually iterate through each property of the original object and create a new object with the same properties. Keep in mind that these alternatives may not handle complex cases like circular references or non-serializable values as well as the `removeDeepReference` function.
Related benchmarks:
Object Clone Lodash vs structuredClone
Lodash cloneDeep vs structuredClone vs JSON.stringify (small object)
teste simple deep copy 2
teste simple deep copy 3
Comments
Confirm delete:
Do you really want to delete benchmark?