Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
teste simple deep copy 3
(version: 0)
https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
Comparing performance of:
json parse vs simple copy vs lodash
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)
lodash
myCopy = _.cloneDeep(MyObject);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
json parse
simple copy
lodash
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):
**Overview** MeasureThat.net is a website where users can create and run JavaScript microbenchmarks to compare the performance of different approaches. The provided JSON represents a benchmark test case that tests three methods for creating a deep copy of an object: using `JSON.parse(JSON.stringify())`, a custom implementation, and the `cloneDeep()` function from the Lodash library. **Test Case 1: JSON Parse** The first test case uses the `JSON.parse(JSON.stringify())` method to create a deep copy of the `MyObject`. This approach is simple and widely supported by most browsers. However, it has some limitations: * It only works with objects that can be serialized to JSON, which may not be the case for all JavaScript objects. * It creates a shallow copy if the object contains circular references, as it uses the `JSON.stringify()` method without any options. Pros: Easy to implement and widely supported. Cons: May not work correctly with all types of objects or create a deep copy in cases where JSON serialization is not possible. **Test Case 2: Simple Copy** The second test case uses a custom implementation to create a deep copy of the `MyObject`. This approach provides more control over the copying process, as it: * Creates a new object and copies each property individually. * Handles circular references by removing them from the original object. Pros: Provides more control over the copying process and can handle circular references. Cons: May be slower than other approaches due to its custom implementation. **Test Case 3: Lodash Clone Deep** The third test case uses the `cloneDeep()` function from the Lodash library to create a deep copy of the `MyObject`. This approach is often used in production code and provides: * A robust implementation that can handle complex objects and circular references. * Better performance compared to custom implementations. Pros: Robust implementation, handles complex objects, and provides better performance. Cons: Requires including the Lodash library, which may not be desirable for all projects. **Library: Lodash** The `cloneDeep()` function is part of the Lodash library, a popular utility library that provides various functions for functional programming tasks. The `cloneDeep()` function creates a deep copy of an object by recursively copying its properties and values. This approach is often used in production code to ensure data integrity and avoid unexpected behavior due to stale data. **Special JS Feature: No** There are no special JavaScript features or syntaxes being tested in this benchmark. **Alternatives** Some alternative approaches for creating a deep copy of an object include: * Using the `Array.prototype.slice()` method to create a shallow copy. * Using the `Object.assign()` method to create a new object and assign properties from the original object. * Using a library like Immutable.js, which provides immutable data structures that can be used to create deep copies. Keep in mind that each approach has its own trade-offs and may not provide the best performance or behavior for all use cases.
Related benchmarks:
Object Clone Lodash vs structuredClone
Lodash cloneDeep vs structuredClone vs JSON.stringify (small object)
teste simple deep copy
teste simple deep copy 2
Comments
Confirm delete:
Do you really want to delete benchmark?