Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Test copy methods
(version: 1)
Comparing performance of:
structuredClone vs JSON vs cloneDeep
Created:
2 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.6.2/lodash.min.js'></script>
Script Preparation code:
var voucher = { "single": false, "applyOverTaxes": false, "discountType": "1", "value": 100, "minimumAmount": 0, "quantity": 1, "suffixLength": 8, "code": "IF5R0AFX", "vertical": [], "allowedPaymentMethods": [], "allowedBanks": [], "types": [], "partners": [], "startDate": "2020-10-21T00:00:00.000Z", "endDate": "2020-10-21T00:00:00.000Z", "applyed": false }
Tests:
structuredClone
const copy = window.structuredClone(voucher);
JSON
const copy = JSON.parse(JSON.stringify(voucher));
cloneDeep
const copy = _.cloneDeep(voucher)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
structuredClone
JSON
cloneDeep
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 OPR/106.0.0.0
Browser/OS:
Opera 106 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
structuredClone
322351.8 Ops/sec
JSON
768581.4 Ops/sec
cloneDeep
858569.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided JSON benchmark. The benchmark is testing three different methods to create a copy of an object: `structuredClone`, `JSON.parse(JSON.stringify())`, and `_.cloneDeep`. The test cases are comparing the performance and efficiency of these three methods on various browsers (Opera 106). **Options Being Compared** 1. **structuredClone**: This method creates a shallow clone of the given object. It's a relatively new method introduced in ECMAScript 2020. 2. **JSON.parse(JSON.stringify())**: This method uses JSON serialization to create a deep copy of the object. However, it has some limitations and potential issues with circular references. 3. **_.cloneDeep** (using Lodash library): This method creates a deep clone of the given object. It's a popular utility function for creating deep copies. **Pros and Cons of Each Approach** 1. **structuredClone**: * Pros: Efficient, accurate, and modern. It's designed to create shallow clones. * Cons: Not supported in older browsers (before ECMAScript 2020), and it might not work correctly with circular references. 2. **JSON.parse(JSON.stringify())**: * Pros: Widely supported, easy to implement, and works well for simple objects. * Cons: Can be slow for large objects, may fail with circular references, and doesn't support custom object properties. 3. **_.cloneDeep** (Lodash library): * Pros: Robust, efficient, and widely supported. It can handle deep cloning of complex objects. * Cons: Requires including the Lodash library, which might add extra overhead. **Library and Its Purpose** The `JSON.parse(JSON.stringify())` method uses JSON serialization to create a copy of the object. However, this approach is not ideal because it: * Can be slow for large objects * Fails with circular references (since it only serializes the object's structure, not its contents) * Doesn't support custom object properties The `_.cloneDeep` method from Lodash library is designed to create a deep clone of the given object. It handles complex cloning scenarios and supports various data types. **Special JS Feature or Syntax** None mentioned in this benchmark. The methods being tested are all standard JavaScript functions or utility libraries (Lodash). **Other Alternatives** For creating copies of objects, other alternatives include: * `Object.assign()` with a custom object * `Array.prototype.slice()` for arrays * `String.prototype.split()` and `String.prototype.concat()` for strings Keep in mind that these alternatives might not be as efficient or robust as the methods being tested (structuredClone, JSON.parse(JSON.stringify()), and _.cloneDeep). In conclusion, this benchmark is testing the performance of different methods for creating copies of objects. The results will help users understand which method to use depending on their specific requirements and constraints.
Related benchmarks:
cloneDeep vs spread object
structuredClone vs spread
_.cloneDeep vs structuredClone vs spread
object copy bench
Comments
Confirm delete:
Do you really want to delete benchmark?