Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash.isEqual vs fast-deep-equal vs JSON.stringify
(version: 0)
Comparing performance of:
lodash vs fast-deep-equal vs JSON.stringify
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.20/lodash.min.js"></script>
Script Preparation code:
function equal(a, b) { if (a === b) return true; if (a && b && typeof a == 'object' && typeof b == 'object') { if (a.constructor !== b.constructor) return false; var length, i, keys; if (Array.isArray(a)) { length = a.length; if (length != b.length) return false; for (i = length; i-- !== 0;) if (!equal(a[i], b[i])) return false; return true; } if (a.constructor === RegExp) return a.source === b.source && a.flags === b.flags; if (a.valueOf !== Object.prototype.valueOf) return a.valueOf() === b.valueOf(); if (a.toString !== Object.prototype.toString) return a.toString() === b.toString(); keys = Object.keys(a); length = keys.length; if (length !== Object.keys(b).length) return false; for (i = length; i-- !== 0;) if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false; for (i = length; i-- !== 0;) { var key = keys[i]; if (!equal(a[key], b[key])) return false; } return true; } // true if both NaN, false otherwise return a!==a && b!==b; }; // 1 level deep var data = [ { description: 'equal numbers', value1: 1, value2: 1, equal: true, }, { description: 'not equal numbers', value1: 1, value2: 2, equal: false, }, { description: 'number and array are not equal', value1: 1, value2: [], equal: false, }, { description: '0 and null are not equal', value1: 0, value2: null, equal: false, }, { description: 'equal strings', value1: 'a', value2: 'a', equal: true, }, { description: 'big object', value1: { prop1: 'value1', prop2: 'value2', prop3: 'value3', prop4: { subProp1: 'sub value1', subProp2: { subSubProp1: 'sub sub value1', subSubProp2: [ 1, 2, { prop2: 1, prop: 2 }, 4, 5, ], }, }, prop5: 1000, prop6: new Date(2016, 2, 10), }, value2: { prop5: 1000, prop3: 'value3', prop1: 'value1', prop2: 'value2', prop6: new Date('2016/03/10'), prop4: { subProp2: { subSubProp1: 'sub sub value1', subSubProp2: [ 1, 2, { prop2: 1, prop: 2 }, 4, 5, ], }, subProp1: 'sub value1', }, }, equal: true, }, ];
Tests:
lodash
data.forEach((item) => { _.isEqual(item.value1, item.value2); });
fast-deep-equal
data.forEach((item) => { equal(item.value1, item.value2); });
JSON.stringify
data.forEach((item) => { JSON.stringify(item.value1) === JSON.stringify(item.value2); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash
fast-deep-equal
JSON.stringify
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash
632390.3 Ops/sec
fast-deep-equal
2160949.0 Ops/sec
JSON.stringify
350095.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided benchmark compares the performance of three functions: `lodash.isEqual`, `fast-deep-equal`, and `JSON.stringify`. The goal is to determine which function is faster for comparing two values. **Options Compared** 1. **Lodash's isEqual**: A popular utility function that checks if two values are equal, considering various types such as objects, arrays, numbers, strings, dates, and more. 2. **Fast-Deep-Equal**: A lightweight function designed to compare two values deeply, handling complex data structures like nested objects and arrays. 3. **JSON.stringify**: A built-in JavaScript method that converts a value into a JSON string, which can be used for comparison. **Pros and Cons of Each Approach** 1. **Lodash's isEqual**: * Pros: Comprehensive comparison, handles various types, and is widely used. * Cons: Can be slow due to its complexity, especially for large data structures. 2. **Fast-Deep-Equal**: * Pros: Lightweight, fast, and specifically designed for deep comparisons. * Cons: May not handle all edge cases (e.g., NaN), and its implementation might not be as comprehensive as Lodash's isEqual. 3. **JSON.stringify**: * Pros: Simple, fast, and easy to implement. * Cons: Only compares values by converting them to strings, which may lead to inaccurate results for complex data structures. **Library and Purpose** The `lodash` library provides a wide range of utility functions, including `isEqual`. The `fast-deep-equal` function is not a part of any popular JavaScript libraries, but it's likely that its implementation was created specifically for this benchmark. **Benchmark Results** The provided benchmark results show the execution speed of each function for the given test cases. The fastest execution time is achieved by `JSON.stringify`, followed closely by `fast-deep-equal`. Lodash's isEqual takes the third position, indicating that it may not be the best choice for this specific use case. Keep in mind that these results might vary depending on the specific hardware, software configuration, and test cases used.
Related benchmarks:
Lodash.isEqual vs JSON.stringify Equality Comparison for Shallow Array of Strings when comparison is not equal.
Lodash.isEqual vs JSON.stringify Equality Comparison for Shallow Array of Strings. Lodash v 4.17.11
Lodash.isEqual vs JSON.stringify Equality Comparison for 1000 length array
JSON Stringify vs Object Keys Properties in two objects comparison
Lodash.isEqual vs JSON.stringify Equality Comparison for Shallow Object of Strings.
Comments
Confirm delete:
Do you really want to delete benchmark?