Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Deep object equal
(version: 0)
Compares deeply two objects
Comparing performance of:
Deep equal vs Lodash isEqual
Created:
4 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script> const equal = (a, b) => { if (a === b || isNaN(a) === isNaN(b)) return true; if (a && b && typeof a === 'function' && typeof b === 'function') { return a.toString() === b.toString(); } if (a && b && typeof a === 'object' && typeof b === 'object') { if (a.constructor !== b.constructor) return false; let length, index; if (Array.isArray(a)) { length = a.length; if (length !== b.length) return false; index = length; while (index--) { if (!equal(a[index], b[index])) return false; } return true; } const keys = Object.keys(a); length = keys.length; if (length !== Object.keys(b).length) return false; index = length; while (index--) { if (!Object.prototype.hasOwnProperty.call(b, keys[index])) return false; } index = length; while (index--) { const key = keys[index]; if (!equal(a[key], b[key])) return false; } return true; } return false; }; </script> <script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Tests:
Deep equal
const a = { a: 123, b: true, c: () => console.log('test'), d: [ 1, 2, 3, { t: 'test', k: [{}, [9, 8, 7], {} ] }, [], {}, { a: 'text', b: false } ] }; const b = { a: 123, b: true, c: () => console.log('test'), d: [ 1, 2, 3, { t: 'test', k: [{}, [9, 8, 7], {} ] }, [], {}, { a: 'text', b: false } ] }; equal(a, b);
Lodash isEqual
const a = { a: 123, b: true, c: () => console.log('test'), d: [ 1, 2, 3, { t: 'test', k: [{}, [9, 8, 7], {} ] }, [], {}, { a: 'text', b: false } ] }; const b = { a: 123, b: true, c: () => console.log('test'), d: [ 1, 2, 3, { t: 'test', k: [{}, [9, 8, 7], {} ] }, [], {}, { a: 'text', b: false } ] }; _.isEqual(a, b);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Deep equal
Lodash isEqual
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):
I'll break down the provided benchmark and explain what's being tested, compared, and their pros/cons. **Benchmark Definition** The provided JSON represents a JavaScript benchmark for testing object equality between two deeply nested objects. **Script Preparation Code** The script preparation code is null, which means that no additional setup or initialization is required before running the benchmark. **Html Preparation Code** The HTML preparation code includes two `<script>` tags: 1. The first tag loads the `lodash.js` library from a CDN, which provides the `_` alias for accessing its functions. 2. The second tag defines a simple JavaScript function `equal(a, b)` that checks if two objects are deeply equal. This function is used to compare the test cases. **Individual Test Cases** There are two test cases: 1. **Deep equal**: This test case compares two identical objects `a` and `b`. 2. **Lodash isEqual**: This test case uses the `_` alias from the loaded Lodash library to call its `isEqual(a, b)` function, which also checks for deep object equality. **Comparison** The benchmark tests two approaches: 1. **Custom implementation (equal function)**: The `equal` function is defined in the HTML preparation code and is used to compare objects directly. 2. **Lodash isEqual**: The `_` alias from Lodash is used to call its `isEqual(a, b)` function, which also checks for deep object equality. **Pros/Cons of each approach** 1. **Custom implementation (equal function)**: * Pros: Simple, lightweight, and self-contained. * Cons: May not be as efficient or comprehensive as a dedicated library like Lodash. 2. **Lodash isEqual**: * Pros: Well-tested, efficient, and provides additional features beyond simple object equality (e.g., handling arrays and functions). * Cons: Requires loading the Lodash library, which adds overhead. **Other considerations** 1. **Special JS feature or syntax**: None mentioned in this benchmark. 2. **Library usage**: Lodash is used for its `isEqual` function. 3. **Device platform**: The benchmark runs on a Linux desktop environment with Chrome 96 browser. **Alternatives** If you need to test object equality, consider the following alternatives: 1. **Built-in JavaScript methods**: Use built-in methods like `Object.is()` (available in modern browsers) or create your own implementation using recursion and shallow/descent comparison. 2. **Other libraries**: Depending on your specific needs, other libraries like `lodash` or `immer` might provide more features or efficiency for object equality checks. Keep in mind that the choice of implementation depends on your project's requirements, performance constraints, and personal preference.
Related benchmarks:
lodash deepEqual vs fast-deep-equal v2
Deep object equalA and equalB
lodash.isEqual vs fast-deep-equal vs stable-hash-compare
lodash.isEqual vs fast-deep-equal SHALLOW
Comments
Confirm delete:
Do you really want to delete benchmark?