Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
javascript deep copy
(version: 0)
slice vs JSON.parse && JSON.stringify
Comparing performance of:
sliceDeepCopy vs jsonDeepCopy
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
Script Preparation code:
function sliceDeepCopy(obj) { return obj.slice() } function jsonDeepCopy(o) { return JSON.parse(JSON.stringify(o)); } var dimensions = [{ "dimensions": [{ "runtime": { "common": { "client": null, "server": null } } }, { "device": { "android": null, "blackberry": null, "iemobile": null, "iphone": null, "ipad": null, "kindle": null, "opera-mini": null, "palm": null } }, { "environment": { "development": { "dev": null, "test": null }, "production": { "stage": null, "prod": null } } }, { "lang": { "ar": { "ar-JO": null, "ar-MA": null, "ar-SA": null, "ar-EG": null }, "bn": { "bn-IN": null }, "ca": { "ca-ES": null }, "cs": { "cs-CZ": null }, "da": { "da-DK": null }, "de": { "de-AT": null, "de-DE": null }, "el": { "el-GR": null }, "en": { "en-AU": null, "en-BG": null, "en-CA": null, "en-GB": null, "en-GY": null, "en-HK": null, "en-IE": null, "en-IN": null, "en-MY": null, "en-NZ": null, "en-PH": null, "en-SG": null, "en-US": null, "en-ZA": null }, "es": { "es-AR": null, "es-BO": null, "es-CL": null, "es-CO": null, "es-EC": null, "es-ES": null, "es-MX": null, "es-PE": null, "es-PY": null, "es-US": null, "es-UY": null, "es-VE": null }, "fi": { "fi-FI": null }, "fr": { "fr-BE": null, "fr-CA": null, "fr-FR": null, "fr-GF": null }, "hi": { "hi-IN": null }, "hu": { "hu-HU": null }, "id": { "id-ID": null }, "it": { "it-IT": null }, "ja": { "ja-JP": null }, "kn": { "kn-IN": null }, "ko": { "ko-KR": null }, "ml": { "ml-IN": null }, "mr": { "mr-IN": null }, "ms": { "ms-MY": null }, "nb": { "nb-NO": null }, "nl": { "nl-BE": null, "nl-NL": null, "nl-SR": null }, "pl": { "pl-PL": null }, "pt": { "pt-BR": null }, "ro": { "ro-RO": null }, "ru": { "ru-RU": null }, "sv": { "sv-SE": null }, "ta": { "ta-IN": null }, "te": { "te-IN": null }, "th": { "th-TH": null }, "tr": { "tr-TR": null }, "vi": { "vi-VN": null }, "zh": { "zh-Hans": { "zh-Hans-CN": null }, "zh-Hant": { "zh-Hant-HK": null, "zh-Hant-TW": null } } } }] }]
Tests:
sliceDeepCopy
var dimensionsCopy = sliceDeepCopy(dimensions);
jsonDeepCopy
var dimensionsCopy = jsonDeepCopy(dimensions);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
sliceDeepCopy
jsonDeepCopy
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):
I'll break down the provided benchmark definition and explain what's being tested, compared, and their pros/cons. **Benchmark Definition** The test compares two methods for creating a deep copy of an object: `sliceDeepCopy` and `jsonDeepCopy`. 1. **`sliceDeepCopy`**: This method uses the `Array.prototype.slice()` method to create a shallow copy of the object. However, since arrays are used extensively in the `dimensions` object, this approach will not work for deep copying, as it only creates a new array reference. 2. **`jsonDeepCopy`**: This method uses `JSON.parse(JSON.stringify(o))`, which is a popular and widely-used way to create a deep copy of an object in JavaScript. **Comparison** The benchmark tests the performance of these two methods on the `dimensions` object. **Pros/Cons** 1. **`sliceDeepCopy`**: * Pros: Simple, easy to understand, and works for shallow copies. * Cons: Fails for deep copying, as it only creates a new array reference. 2. **`jsonDeepCopy`**: * Pros: Works for deep copying, uses a widely-known and tested approach. * Cons: Can be slower due to the overhead of JSON parsing and stringification. **Benchmark Results** The latest benchmark results show that: 1. **`sliceDeepCopy`**: Executes approximately 5241797 times per second on Chrome Mobile 104 (Android). 2. **`jsonDeepCopy`**: Executes approximately 42376.89 times per second on Chrome Mobile 104 (Android). These results suggest that `jsonDeepCopy` is slower than `sliceDeepCopy`, which might be due to the overhead of JSON parsing and stringification. However, it's essential to note that while `jsonDeepCopy` may be slower, it provides a reliable way to create deep copies of objects in JavaScript. If you need to perform this operation frequently, you may want to consider optimizing one or both methods for better performance. Keep in mind that these results are specific to the test case and might not generalize to other scenarios or environments.
Related benchmarks:
Array.prototype.slice Versus Spread Operator
Array.prototype.slice vs spread operator copy
Spread1 vs slice1
Javascript array cloning slice vs spread
Array.prototype.slice vs spread operator to copy array
Comments
Confirm delete:
Do you really want to delete benchmark?