Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash vs JSON22
(version: 0)
Comparing performance of:
lodash vs JSON
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/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: { stringify: { stringify: { stringify: { stringify: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' }, parse: 'JSON.parse() method parses a JSON string...' }, parse: 'JSON.parse() method parses a JSON string...' }, parse: 'JSON.parse() method parses a JSON string...' }, parse: 'JSON.parse() method parses 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
myCopy = _.cloneDeep(MyObject);
JSON
myCopy = JSON.parse(JSON.stringify(MyObject));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash
JSON
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
Browser/OS:
Chrome 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash
218552.4 Ops/sec
JSON
219596.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help you understand what's being tested in this benchmark. **Benchmark Overview** The benchmark compares the performance of two approaches for creating a deep copy of an object: using the Lodash library (`lodash`) and the built-in `JSON.parse(JSON.stringify())` method. The test case is designed to measure the execution speed of these two approaches on a specific input object, which contains nested objects and arrays. **Options Compared** The benchmark compares the following options: 1. **Lodash (`.cloneDeep()`)**: Uses the Lodash library's `cloneDeep()` function to create a deep copy of the input object. 2. **Built-in JSON.parse(JSON.stringify())**: Uses the built-in JavaScript method `JSON.parse(JSON.stringify())` to create a deep copy of the input object. **Pros and Cons** 1. **Lodash (.cloneDeep())**: * Pros: Lodash is a popular and widely-used library that provides efficient and reliable data manipulation functions. * Cons: Requires an external dependency (the Lodash library) to be included in the test environment, which may add overhead. 2. **Built-in JSON.parse(JSON.stringify())**: * Pros: Built-in and lightweight, no additional dependencies required. * Cons: Can lead to issues if the input object contains non-serializable properties or cyclic references. **Library Usage** The benchmark uses the Lodash library (`lodash.js`), which is a popular utility library for JavaScript. The `cloneDeep()` function is used to create a deep copy of the input object, and it's likely that this function provides a efficient implementation using techniques like prototype chain manipulation and caching. **Special JS Features or Syntax** The benchmark does not explicitly use any special JavaScript features or syntax beyond the standard ECMAScript 2015 (ES6) language features. However, it's worth noting that the `JSON.parse(JSON.stringify())` method can be sensitive to certain edge cases, such as: * Non-serializable properties: If the input object contains non-serializable properties (e.g., functions or undefined values), the `JSON.parse()` method may throw an error. * Cyclic references: If the input object contains cyclic references (i.e., objects that reference themselves indirectly), the `JSON.parse()` method may enter an infinite recursion and cause a stack overflow. **Alternatives** Other alternatives for creating deep copies of JavaScript objects include: 1. **For...in loops**: Can be used to create a deep copy of an object by iterating over its properties and recursively cloning each value. 2. **Array.prototype.slice()**: Can be used to create a shallow copy of an array, but may not work correctly if the input array contains complex or non-serializable data structures. 3. **Object.assign()**: Can be used to create a shallow copy of an object, but may not work correctly if the input object contains complex or non-serializable data structures. Keep in mind that these alternatives may have different performance characteristics and error handling compared to the Lodash library or the built-in `JSON.parse(JSON.stringify())` method.
Related benchmarks:
lodash clonedeep vs json.parse(stringify()) vs recursivecopy vs shallowcopy
lodash clonedeep vs json.parse(stringify()) vs recursivecopy new big
Plain Json: lodash clonedeep vs json.parse(stringify())
Lodash cloneDeep vs structuredClone vs recursiveDeepCopy vs JSON clone with a more deep test
Comments
Confirm delete:
Do you really want to delete benchmark?