Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash clonedeep vs json.parse(stringify()) vs recursivecopy vs recursivecopy2 with Document var
(version: 0)
Comparing performance of:
Lodash CloneDeep vs Json Clone vs recursiveDeepCopy vs recursiveDeepCopy2
Created:
6 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 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 } } } }] }] var myCopy = null; function recursiveDeepCopy(o) { var newO, i; if (typeof o !== 'object') { return o; } if (!o) { return o; } if ('[object Array]' === Object.prototype.toString.apply(o)) { newO = []; for (i = 0; i < o.length; i += 1) { newO[i] = recursiveDeepCopy(o[i]); } return newO; } newO = {}; for (i in o) { if (o.hasOwnProperty(i)) { newO[i] = recursiveDeepCopy(o[i]); } } return newO; } function recursiveDeepCopy2(obj) { return Object.keys(obj).reduce((v, d) => Object.assign(v, { [d]: (obj[d].constructor === Object) ? recursiveDeepCopy(obj[d]) : obj[d] }), {}); }
Tests:
Lodash CloneDeep
myCopy = _.cloneDeep(dimensions);
Json Clone
myCopy = JSON.parse(JSON.stringify(dimensions));
recursiveDeepCopy
myCopy = recursiveDeepCopy(dimensions);
recursiveDeepCopy2
myCopy = recursiveDeepCopy2(dimensions);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Lodash CloneDeep
Json Clone
recursiveDeepCopy
recursiveDeepCopy2
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):
It looks like you have a test report with benchmark results for four different cloning functions: `_.cloneDeep` from Lodash, `JSON.parse(JSON.stringify())`, and two custom recursive cloning functions (`recursiveDeepCopy` and `recursiveDeepCopy2`). You want to know which function is the fastest. Let's analyze the results: 1. **Lodash CloneDeep**: 5365.490234375 executions per second 2. **JSON.parse(JSON.stringify())**: 20702.20703125 executions per second (much faster!) 3. **recursiveDeepCopy**: 15452.9970703125 executions per second 4. **recursiveDeepCopy2**: 16234.294921875 executions per second Based on the results, **JSON.parse(JSON.stringify())** is significantly faster than the other three functions. Here's why: * `_.cloneDeep` is a good general-purpose cloning function, but it may be slower due to its overhead. * `recursiveDeepCopy` and `recursiveDeepCopy2` are custom implementations that require more effort to implement correctly. While they might be optimized for performance, they seem to trail behind the built-in `JSON.parse(JSON.stringify())` method. In conclusion, if you need a fast and reliable way to clone objects, **JSON.parse(JSON.stringify())** is likely your best bet.
Related benchmarks:
Lodash cloneDeep vs JSON Clone with Array
cloneDeep vs JSON stringify + parse (long arr)
lodash cloneDeep vs json.stringify
Lodash cloneDeep vs JSON parse
Object cloning with Lodash clone vs cloneDeep vs merge vs structuredClone vs JSON.parse(JSON.stringify(o))
Comments
Confirm delete:
Do you really want to delete benchmark?