Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash.isEqual vs fast-deep-equal SHALLOW
(version: 0)
Comparing performance of:
lodash vs fast-deep-equal
Created:
2 years 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', }, value2: { prop3: 'value3', prop1: 'value1', prop2: 'value2', }, equal: true, }, ];
Tests:
lodash
data.forEach((item) => { _.isEqual(item.value1, item.value2); });
fast-deep-equal
data.forEach((item) => { equal(item.value1, item.value2); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash
fast-deep-equal
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 explaining the provided benchmark. **Benchmark Overview** The benchmark compares the performance of two functions: `_.isEqual` from Lodash and `equal` defined in the script preparation code, which is implemented by the user. The test cases use an array of objects (`data`) to measure the execution time of each function. **Options Compared** Two options are compared: 1. **Lodash's _.isEqual**: This is a built-in function in Lodash that checks for deep equality between two values. 2. **Custom `equal` function**: This is the function defined in the script preparation code, which performs a custom implementation of deep equality checking. **Pros and Cons** **Lodash's _.isEqual:** Pros: * Widely used and well-tested library * Supports various data types (arrays, objects, numbers, strings) * Can handle cyclic references and NaN values Cons: * May be overkill for simple use cases or small datasets * Requires an additional dependency (Lodash) **Custom `equal` function:** Pros: * Lightweight and compact implementation * Only depends on JavaScript's built-in functions * Easy to understand and modify for specific use cases Cons: * May not handle all edge cases or data types * Requires manual implementation of equality checks for certain data types (e.g., arrays, objects) **Other Considerations** The custom `equal` function uses a recursive approach to check for equality between two values. It handles arrays by iterating over each element and checking for equality using the same approach. For objects, it uses `Object.keys()` to get an array of property names and then iterates over the properties, checking for equality using the `Object.prototype.hasOwnProperty.call()` method. **Library: Lodash** Lodash is a popular JavaScript library that provides a collection of useful functions for tasks such as data manipulation, string manipulation, and more. The `_.isEqual` function is one of its most widely used functions, which checks for deep equality between two values. **Special JS Feature/Syntax** None mentioned in the provided benchmark. **Alternative Approaches** Other approaches to implement a custom `equal` function could include: * Using a library like `lodash-deep-equal` (a more lightweight version of Lodash's deep equality checking) * Implementing a custom equality checker using only JavaScript's built-in functions and data structures * Using a different algorithm or approach, such as a recursive descent parser These alternatives would require additional consideration of the trade-offs between simplicity, performance, and code size.
Related benchmarks:
fast compare test w/lodash 3
lodash deepEqual vs fast-deep-equal v2
lodash.isEqual vs fast-deep-equal vs stable-hash-compare
lodash vs fast-equals vs fast-deep-equal
lodash.isEqual vs fast-deep-equal vs local 2
Comments
Confirm delete:
Do you really want to delete benchmark?