Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
custom_deepEqual vs. lodash.isEqual
(version: 0)
Comparing performance of:
lodash vs custom-deepEqual vs JSON.stringify()
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
Script Preparation code:
function deepEqual(obj1, obj2){ // Initialisation let obj1Keys = Object.keys(obj1); let obj2Keys = Object.keys(obj2); // Compare le nombre de propriétés if(obj1Keys.length != obj2Keys.length) return false; // Compare le nom des clés de chaque propriété let set = new Set([...obj1Keys, ...obj2Keys]); if(set.size != obj1Keys.length) return false; // Compare les valeurs { // Compare le type de chaque valeur let objValues = obj1Keys.map(key =>[obj1[key], obj2[key]]) let sameType = objValues.every( value => typeof value[0] === typeof value[1]); if(!sameType) return false; // Compare directement les valeurs let valueTypes = ['number', 'string', 'boolean', 'symbol', 'undefined', 'bigint']; let allTypes = [...valueTypes, 'null', 'object']; // On trie le tableau pour comparer en priorité les types valeurs, puis les types références (qui impliquent une récursion) let objValuesTypeSorted = objValues.sort((a, b) => allTypes.indexOf(typeof a[0]) - allTypes.indexOf(typeof b[0])); // On utilise every() pour sortir de la méthode dès qu'une différence est relevée var sameValues = objValuesTypeSorted.every(values => { // Compare les types valeurs if(valueTypes.includes(typeof values[0])) return values[0] === values[1]; // Compare les types objets else { if (values[0] === null && values[1] === null) return true; else if(values[0] === null && values[1] !== null || values[1] === null && values[0] !== null) return false; else return deepEqual(values[0], values[1]); } }) } if(!sameValues) return false; return true; } // 1 level deep window.foo1 = { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 1, b: 2 } } }; window.bar1 = { a: 1, b: 3, c: { a: 1, b: 2, c: { a: 1, b: 2 } } }; // 2 levels deep window.foo2 = { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 1, b: 2 } } }; window.bar2 = { a: 1, b: 2, c: { a: 1, b: 3, c: { a: 1, b: 2 } } }; // 3 levels deep window.foo3 = { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 1, b: 2 } } }; window.bar3 = { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 1, b: 4 } } };
Tests:
lodash
_.isEqual(window.foo1, window.bar1)
custom-deepEqual
deepEqual(window.foo1, window.bar1);
JSON.stringify()
JSON.stringify(window.foo1) === JSON.stringify(window.bar1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash
custom-deepEqual
JSON.stringify()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0
Browser/OS:
Chrome 138 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash
1226577.1 Ops/sec
custom-deepEqual
1282822.8 Ops/sec
JSON.stringify()
1782061.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll explain the benchmark test in detail. **What is being tested?** The provided JSON represents a JavaScript microbenchmarking test created on MeasureThat.net. The test compares three different approaches for checking equality between two objects: `lodash.isEqual`, a custom implementation (`deepEqual`), and string comparison using `JSON.stringify()`. **Options compared:** 1. **Lodash's `isEqual` method**: A popular library function for deep object comparison. 2. **Custom implementation (`deepEqual`)**: A bespoke function for checking deep object equality, as shown in the provided JSON. 3. **String comparison with `JSON.stringify()`**: A simple approach that compares two objects by converting them to strings and comparing those strings. **Pros and cons of each approach:** 1. **Lodash's `isEqual` method**: * Pros: Well-tested, efficient, and widely adopted. * Cons: Requires an external dependency (the Lodash library), which might not be desirable in all cases. 2. **Custom implementation (`deepEqual`)**: * Pros: Allows for customization of the comparison logic, can be more efficient than `JSON.stringify()`, and does not require an external dependency. * Cons: Requires manual implementation and maintenance, which can lead to errors or inconsistencies. 3. **String comparison with `JSON.stringify()`**: * Pros: Simple, lightweight, and fast. * Cons: May not accurately compare complex objects, as the resulting strings might not represent the original object structure. **Library usage:** In this benchmark, Lodash's `isEqual` method is used in one of the test cases. Lodash provides a convenient way to perform deep object comparisons, which can be an efficient and reliable approach for this task. **Special JS features or syntax:** None are explicitly mentioned in the provided code or JSON. However, it's worth noting that modern JavaScript versions (ES6+), which are commonly used, offer advanced features like `Object.prototype.toString()` and `JSON.stringify()`, which might be relevant to understanding the comparison logic. **Other alternatives:** 1. **Built-in `===` operator**: In some cases, the built-in `===` operator can be used for simple object comparisons. 2. **Other libraries or functions**: Other libraries like `immer` or custom functions implementing deep equality checks could also be considered as alternatives to Lodash's `isEqual`. In summary, this benchmark tests three different approaches for checking equality between two objects: a popular library (`lodash.isEqual`), a custom implementation (`deepEqual`), and string comparison with `JSON.stringify()`. The choice of approach depends on the specific requirements and constraints of the project.
Related benchmarks:
my vs fast-deep-equal vs lodash
Yepo_deepEqual vs. lodash.isEqual v1.1
Another lodash isEqual test
Lodash vs Native JS isEqual
Comments
Confirm delete:
Do you really want to delete benchmark?