Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash clonedeep vs json.parse(stringify()) vs recursivecopy vs shallowcopy
(version: 0)
Comparing performance of:
Lodash CloneDeep vs Json Clone vs recursiveDeepCopy vs shalow copy
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 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...', vetor: [ { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' }, { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' }, { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' } ], jayson: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...', jayson: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...', vetor: [ { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' }, { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' }, { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' } ], jayson: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' } } } } }; 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; }
Tests:
Lodash CloneDeep
myCopy = _.cloneDeep(MyObject);
Json Clone
myCopy = JSON.parse(JSON.stringify(MyObject));
recursiveDeepCopy
myCopy = recursiveDeepCopy(MyObject);
shalow copy
myCopy = {...MyObject};
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
shalow copy
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36
Browser/OS:
Chrome 144 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash CloneDeep
337017.1 Ops/sec
Json Clone
499900.5 Ops/sec
recursiveDeepCopy
2047386.1 Ops/sec
shalow copy
14898601.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into explaining the provided benchmark. **Benchmark Overview** The test compares the performance of four different methods for creating deep copies of JavaScript objects: 1. `_.cloneDeep` from Lodash 2. `JSON.parse(JSON.stringify())` 3. `recursiveDeepCopy` 4. Shallow copy using `{...}` **Lodash CloneDeep (`_ = require('lodash'); var myCopy = _.cloneDeep(MyObject);`)** * **Purpose**: The `_.cloneDeep()` function creates a deep copy of the input object, recursively copying all properties and nested objects. * **Pros**: Convenient and easy to use. Provides a robust way to create deep copies. * **Cons**: May have performance implications due to the overhead of cloning and recursion. **JSON.parse(JSON.stringify())** * **Purpose**: This method uses `JSON.stringify()` to serialize the input object into a JSON string and then parses it back using `JSON.parse()`. Since JavaScript is capable of automatically serializing objects, this method should theoretically create an exact copy. * **Pros**: Simple and straightforward. Works well for simple objects or when objects are not deeply nested. * **Cons**: Does not work correctly with complex data structures (e.g., circular references) since `JSON.parse()` will throw an error. **recursiveDeepCopy** * **Purpose**: This is a custom function that recursively clones the input object by creating new copies of each property and its nested objects. * **Pros**: Allows for complete control over the cloning process. Handles complex data structures correctly. * **Cons**: Requires manual implementation, which can be error-prone and cumbersome. **Shallow Copy (`myCopy = {...MyObject};`)** * **Purpose**: Creates a new object with the same properties as the original input object but without creating new objects for nested values. * **Pros**: Fast and lightweight. Suitable for simple objects or when speed is crucial. * **Cons**: Does not create deep copies, which means that modifying the copied object will affect the original. **Alternatives** Other methods for creating deep copies of JavaScript objects include: 1. `Object.assign()`: Creates a new object with the same properties as the original input object but without creating new objects for nested values. 2. `Array.prototype.slice()`: Creates a shallow copy of an array (not applicable to objects). 3. Using a library like Immer or Ramda, which provide more advanced data transformation and copying functions. Keep in mind that the choice of method depends on the specific requirements of your project, such as performance needs, data complexity, and code readability.
Related benchmarks:
lodash clonedeep vs json.parse(stringify()) vs recursivecopy new big
Lodash cloneDeep vs structuredClone vs recursiveDeepCopy vs JSON clone with a more deep test
lodash clonedeep vs json.parse(stringify()) vs recursivecopy vs structured clone
lodash clonedeep vs json.parse(stringify()) vs recursivecopy heavy
Comments
Confirm delete:
Do you really want to delete benchmark?