Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
teste simple deep copy 2
(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 = {}; const keys = Object.keys(object) for (let index = 0; index < keys.length; index += 1) { const key = keys[index]; const arr = object[key] newObject[key] = arr.slice(0, arr.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):
**Benchmark Overview** The provided JSON represents a JavaScript microbenchmark test case on the MeasureThat.net website. The benchmark tests two approaches for creating a deep copy of an object: using `JSON.parse(JSON.stringify(MyObject))` and implementing a custom function `removeDeepReference`. **Script Preparation Code** The script preparation code defines an object `MyObject` with a nested array `description`, which contains both boolean, false, undefined, and true values. **Html Preparation Code** The HTML preparation code includes a reference to the Lodash library (version 4.17.5), which is used in one of the test cases. **Test Cases** There are two test cases: 1. **"json parse"`**: This test case creates a deep copy of `MyObject` using `JSON.parse(JSON.stringify(MyObject))`. The `JSON.stringify()` function returns a string representation of the object, and then `JSON.parse()` parses this string back into an object. 2. **"simple copy"`**: This test case creates a deep copy of `MyObject` using a custom function `removeDeepReference`. This function iterates over each property of the object, recursively processes its value, and creates a new object with modified values. **Library: Lodash** The Lodash library is used in the "json parse" test case. Lodash provides a set of utility functions for tasks such as string manipulation, array manipulation, and object manipulation. In this specific case, `JSON.stringify()` is used to convert an object to a string representation, which can be parsed later to recreate the original object. **Special JS Features/ Syntax** There are no special JavaScript features or syntaxes used in these test cases. **Options Compared** The two test cases compare two approaches for creating a deep copy of an object: * **Using `JSON.parse(JSON.stringify(MyObject))`**: This method creates a shallow copy of the object, as it only copies the top-level properties and does not recursively process nested objects. * **Implementing a custom function `removeDeepReference"`**: This method creates a deep copy of the object by recursively processing each property and creating new objects for nested values. **Pros and Cons** Here are some pros and cons of each approach: **Using `JSON.parse(JSON.stringify(MyObject))`**: Pros: * Simple and concise implementation * Fast execution Cons: * Creates a shallow copy, not a deep copy * May not work correctly with complex data structures (e.g., circular references) **Implementing a custom function `removeDeepReference`**: Pros: * Creates a deep copy of the object * Can handle complex data structures (e.g., circular references) Cons: * More complex and verbose implementation * Slower execution compared to the `JSON.parse(JSON.stringify())` method **Other Alternatives** There are other approaches for creating a deep copy of an object in JavaScript, such as: * Using a library like Immer or Ramda, which provide functions for creating immutable copies of objects. * Implementing a custom function using recursion and/or iteration to process nested properties. * Using the `Object.assign()` method with a spread operator (`{...}`) to create a shallow copy of an object. However, the `removeDeepReference` function implemented in this benchmark is a simple and effective example of creating a deep copy using a recursive approach.
Related benchmarks:
Object Clone Lodash vs structuredClone
Lodash cloneDeep vs structuredClone vs JSON.stringify (small object)
teste simple deep copy
teste simple deep copy 3
Comments
Confirm delete:
Do you really want to delete benchmark?