Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Deep equal
(version: 0)
Compares deeply two objects
Comparing performance of:
Compare two equal objects vs Compare two unequal objects
Created:
4 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script> const equal = (a, b) => { if (a === 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>
Tests:
Compare two equal objects
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);
Compare two unequal objects
const a = { a: 123, b: true, c: ()=> console.log('this is a fn call'), 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);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Compare two equal objects
Compare two unequal objects
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Compare two equal objects
1566991.9 Ops/sec
Compare two unequal objects
1688666.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided JSON represents the benchmark test cases for comparing deeply two objects in JavaScript. **Benchmark Definition** The benchmark definition is implemented as a JavaScript function named `equal` that takes two objects, `a` and `b`, as input. The function checks if both objects are equal using various conditions: 1. Primitive value comparison (e.g., numbers, strings) 2. Function comparison (checks if both functions have the same string representation) 3. Object comparison (compares object keys and values recursively) The benchmark definition includes a script preparation code that defines two objects, `a` and `b`, which are used as input for the `equal` function. **Individual Test Cases** There are two test cases: 1. **Compare two equal objects**: This test case creates two identical objects, `a` and `b`, and calls the `equal` function to compare them. 2. **Compare two unequal objects**: This test case creates two different objects, `a` and `b`, with distinct values or properties, and calls the `equal` function to compare them. **Options Compared** In this benchmark, only one option is compared: * Deeply comparing two objects using the `equal` function. **Pros and Cons of the Approach** The approach used in this benchmark has both pros and cons: Pros: * **Robust comparison**: The `equal` function uses multiple conditions to compare objects deeply, which helps ensure accurate results. * **Easy implementation**: The benchmark definition is simple and straightforward, making it easy to understand and maintain. Cons: * **Performance overhead**: The recursive nature of the object comparison might lead to performance issues for large or complex objects. * **Limited support**: This approach only works with JavaScript objects and does not account for other data types (e.g., arrays, primitive values). **Library Used** The `equal` function uses no external libraries, but it relies on built-in JavaScript features like `typeof`, `Object.keys()`, and `Array.isArray()`. **Special JS Features or Syntax** This benchmark does not use any special JavaScript features or syntax. The code is written in standard JavaScript and should be compatible with most modern browsers and environments. **Other Alternatives** Alternative approaches to compare objects deeply include: * Using a library like Lodash (`_isEqual` function) or Ramda (`eq` function) * Implementing a custom comparison function using a different approach (e.g., using a hash table to store object properties) Keep in mind that these alternatives might have their own pros and cons, and the choice of implementation depends on the specific requirements and constraints of your project.
Related benchmarks:
lodash.isEqual vs fast-deep-equal
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?