Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Underscore extended deepClone vs JSON parse
(version: 0)
Comparison of deep cloning 10,000 objects in an array with Underscore deepClone vs JSON parse and stringify.
Comparing performance of:
Underscore extended deepClone vs JSON parse/stringify
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/underscore@1.3.1/underscore-min.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/backbone@1.4.0/backbone-min.min.js"></script>
Script Preparation code:
(function(_, Backbone) { var arrays, basicObjects, deepClone, deepExtend, deepExtendCouple, isBasicObject, __slice = [].slice; deepClone = function(obj) { var func, isArr; if (!_.isObject(obj) || _.isFunction(obj)) { return obj; } if (obj instanceof Backbone.Collection || obj instanceof Backbone.Model) { return obj; } if (_.isDate(obj)) { return new Date(obj.getTime()); } if (_.isRegExp(obj)) { return new RegExp(obj.source, obj.toString().replace(/.*\//, "")); } isArr = _.isArray(obj || _.isArguments(obj)); func = function(memo, value, key) { if (isArr) { memo.push(deepClone(value)); } else { memo[key] = deepClone(value); } return memo; }; return _.reduce(obj, func, isArr ? [] : {}); }; isBasicObject = function(object) { if (object == null) return false; return (object.prototype === {}.prototype || object.prototype === Object.prototype) && _.isObject(object) && !_.isArray(object) && !_.isFunction(object) && !_.isDate(object) && !_.isRegExp(object) && !_.isArguments(object); }; basicObjects = function(object) { return _.filter(_.keys(object), function(key) { return isBasicObject(object[key]); }); }; arrays = function(object) { return _.filter(_.keys(object), function(key) { return _.isArray(object[key]); }); }; deepExtendCouple = function(destination, source, maxDepth) { var combine, recurse, sharedArrayKey, sharedArrayKeys, sharedObjectKey, sharedObjectKeys, _i, _j, _len, _len1; if (maxDepth == null) { maxDepth = 20; } if (maxDepth <= 0) { console.warn('_.deepExtend(): Maximum depth of recursion hit.'); return _.extend(destination, source); } sharedObjectKeys = _.intersection(basicObjects(destination), basicObjects(source)); recurse = function(key) { return source[key] = deepExtendCouple(destination[key], source[key], maxDepth - 1); }; for (_i = 0, _len = sharedObjectKeys.length; _i < _len; _i++) { sharedObjectKey = sharedObjectKeys[_i]; recurse(sharedObjectKey); } sharedArrayKeys = _.intersection(arrays(destination), arrays(source)); combine = function(key) { return source[key] = _.union(destination[key], source[key]); }; for (_j = 0, _len1 = sharedArrayKeys.length; _j < _len1; _j++) { sharedArrayKey = sharedArrayKeys[_j]; combine(sharedArrayKey); } return _.extend(destination, source); }; deepExtend = function() { var finalObj, maxDepth, objects, _i; objects = 2 <= arguments.length ? __slice.call(arguments, 0, _i = arguments.length - 1) : (_i = 0, []), maxDepth = arguments[_i++]; if (!_.isNumber(maxDepth)) { objects.push(maxDepth); maxDepth = 20; } if (objects.length <= 1) { return objects[0]; } if (maxDepth <= 0) { return _.extend.apply(this, objects); } finalObj = objects.shift(); while (objects.length > 0) { finalObj = deepExtendCouple(finalObj, deepClone(objects.shift()), maxDepth); } return finalObj; }; _.mixin({ deepClone: deepClone, isBasicObject: isBasicObject, basicObjects: basicObjects, arrays: arrays, deepExtend: deepExtend }); }).call(this, _, Backbone); var items = [ ...Array(10000).keys() ].map( i => { return { bar: "foo" } });
Tests:
Underscore extended deepClone
_.deepClone(items);
JSON parse/stringify
JSON.parse(JSON.stringify(items));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Underscore extended deepClone
JSON parse/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! The provided JSON represents a benchmark test case for comparing the performance of two approaches: using Underscore's `deepClone` function and using `JSON.parse(JSON.stringify())`. **What is being tested?** In this benchmark, 10,000 objects are created in an array (`items`) with a single property `bar` set to `"foo"`. The test case consists of two individual test cases: 1. **Underscore extended deepClone**: This test case uses Underscore's `deepClone` function to create a deep copy of the `items` array. 2. **JSON parse/stringify**: This test case uses the `JSON.parse(JSON.stringify())` method to create a deep copy of the `items` array. **Options being compared** The two options being compared are: 1. Underscore's `deepClone` function 2. The `JSON.parse(JSON.stringify())` method **Pros and Cons of each approach:** 1. **Underscore's `deepClone` function**: * Pros: + Specifically designed for deep cloning in JavaScript. + Handles complex data structures, such as arrays and objects with nested properties. + Can be customized to suit specific use cases. * Cons: + May have a slightly higher memory footprint due to its object creation mechanism. 2. **JSON parse/stringify**: * Pros: + Simple and straightforward implementation. + No additional dependencies required (other than the built-in `JSON` object). * Cons: + Can be slower for large data structures due to the overhead of parsing and stringifying JSON. + May not handle complex data structures as well as `deepClone`. **Device information** The benchmark results indicate that the tests were run on a Chrome 119 browser on a Mac OS X 10.15.7 device, with an average of: * 1184 executions per second for the `JSON parse/stringify` test * 1036 executions per second for the Underscore extended deepClone test **Conclusion** In this benchmark, Underscore's `deepClone` function appears to outperform the `JSON.parse(JSON.stringify())` method for creating a deep copy of an array with nested objects. However, it's essential to note that these results may vary depending on specific use cases and system configurations.
Related benchmarks:
Comparing deep cloning methods (small object): Lodash <> Custom clone func <> JSON.parse <> structuredClone
Comparing deep cloning methods (array of objects): Lodash <> Custom clone func <> JSON.parse <> structuredClone
Comparing deep cloning methods (large array of objects): Lodash <> Custom clone func <> JSON.parse <> structuredClone
Comparing deep cloning methods (Complex object): Lodash <> Custom clone func <> JSON.parse <> structuredClone
Comments
Confirm delete:
Do you really want to delete benchmark?