Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
big clonedeep
(version: 0)
Comparing performance of:
clonedeep vs structuredClone vs stringify
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:
let bigArray = []; for (i = 0; i < 100000; i++) { bigArray.push([{ first: i, second: `${i} som en sträng`, third: i * 3.14, fourth: [{ i: [i, i+1] }], }]); } var MyObject = { description: 'Creates a deep copy of source, which should be an object or an array.', myNumber: 123456789, myBoolean: true, jayson: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' }, bigobjectArray: bigArray, }; var myCopy = null;
Tests:
clonedeep
myCopy = _.cloneDeep(MyObject);
structuredClone
myCopy = structuredClone(MyObject);
stringify
myCopy = JSON.parse(JSON.stringify(MyObject));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
clonedeep
structuredClone
stringify
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 dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Overview** The benchmark consists of three test cases: `clonedeep`, `structuredClone`, and `stringify`. Each test case aims to measure the performance of different methods for creating a deep copy of an object in JavaScript. **Options Compared** The options compared are: 1. **Deep Cloning**: Using the `_.cloneDeep` method from Lodash, which creates a deep copy of an object. 2. **Structured Cloning**: Using the `structuredClone` function (introduced in ECMAScript 2020), which creates a shallow copy of an object by serializing it to a string and then parsing it back into an object. 3. **Stringification**: Using the `JSON.stringify` method followed by `JSON.parse` to create a deep copy of an object. **Pros and Cons of Each Approach** 1. **Deep Cloning (Lodash's `_cloneDeep`)**: * Pros: Creates a true deep copy, preserving all nested objects and arrays. * Cons: Can be slower than other approaches due to the complexity of the cloning process. 2. **Structured Cloning**: * Pros: Fast and efficient, as it only serializes the object's value and not its prototype chain. * Cons: Creates a shallow copy, which may not preserve all nested objects and arrays. 3. **Stringification**: * Pros: Very fast, as it only involves serialization and parsing of the object's value. * Cons: May not work correctly for certain types of objects (e.g., those with circular references) or may not preserve all properties. **Library and Purpose** 1. `_.cloneDeep` from Lodash: * A utility function that creates a deep copy of an object, preserving all nested objects and arrays. 2. `structuredClone` function (ECMAScript 2020): * A new function introduced in ECMAScript 2020 that creates a shallow copy of an object by serializing it to a string and then parsing it back into an object. **Special JS Features or Syntax** None mentioned in the benchmark definition, but note that `structuredClone` is a relatively new feature in JavaScript, so support for it may be limited in older browsers or environments. **Other Alternatives** 1. **JSON.parse(JSON.stringify(obj))**: Another way to achieve deep cloning using stringification and parsing. 2. **Object.assign()`: Can be used to create a shallow copy of an object by assigning its properties to a new object. 3. **Array.prototype.slice()` or `Array.prototype.slice.call()`: Can be used to create a shallow copy of an array. Keep in mind that these alternatives may have different trade-offs in terms of performance, complexity, and preservation of nested objects and arrays.
Related benchmarks:
Lodash cloneDeep vs JSON Clone with huge object
Lodash cloneDeep vs JSON Clone (very big object)
Lodash cloneDeep vs JSON Clone vs Object Spread
test clonedeep
Comments
Confirm delete:
Do you really want to delete benchmark?