Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Deep object equalA and equalB
(version: 0)
Compares deeply two objects
Comparing performance of:
Deep equalA vs Deep equalB
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script> const equalA = (a, b) => { if (a === b) return true; 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 (!equalA(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--) { const key = keys[index]; if (!equalA(a[key], b[key])) return false; } return true; } return false; }; const equalB = (a, b) => { if (a === b) return true; 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 (!equalB(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 (!equalB(a[keys[index]], b[keys[index]])) return false; } return true; } return false; }; </script>
Tests:
Deep equalA
const a = { a: 123, b: true, c: 'abcd', d: [ 1, 2, 3, { t: 'test', k: [{}, [9, 8, 7], {} ] }, [], {}, { a: 'text', b: false } ] }; const b = { a: 123, b: true, c: 'abcd', d: [ 1, 2, 3, { t: 'test', k: [{}, [9, 8, 7], {} ] }, [], {}, { a: 'text', b: false } ] }; equalA(a, b);
Deep equalB
const a = { a: 123, b: true, c: 'abcd', d: [ 1, 2, 3, { t: 'test', k: [{}, [9, 8, 7], {} ] }, [], {}, { a: 'text', b: false } ] }; const b = { a: 123, b: true, c: 'abcd', d: [ 1, 2, 3, { t: 'test', k: [{}, [9, 8, 7], {} ] }, [], {}, { a: 'text', b: false } ] }; equalB(a, b);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Deep equalA
Deep equalB
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 break down the benchmark and its various components. **Benchmark Definition** The benchmark measures the performance of two identical functions, `equalA` and `equalB`, which compare deeply two objects. The object comparison function is called recursively to check for nested properties and arrays. **Options Compared** There are two variants of the object comparison function: 1. `equalA` 2. `equalB` The main difference between these two functions lies in how they iterate through the object's properties and arrays: * `equalA` uses a single variable, `index`, to iterate through both objects at the same time. * `equalB` uses a separate array to store indices of equal elements. **Pros and Cons** 1. **Single iteration**: `equalA` only iterates through the objects once, which may be more efficient in terms of memory usage and cache locality. However, it also introduces additional overhead due to accessing multiple variables simultaneously. 2. **Parallel iteration**: `equalB` uses a separate array to store indices of equal elements, which can lead to better parallelization and potentially faster execution times. However, this approach may result in increased memory usage and decreased cache efficiency. **Library** There is no explicitly mentioned library in the benchmark definition or test cases. However, both functions use built-in JavaScript features, such as `Object.keys()`, arrays, and recursion, which are part of the standard language. **Special JS Features/Syntax** The benchmark uses recursive function calls to compare objects, which is a common pattern in JavaScript. The `typeof` operator is used to check the type of variables, and the `Array.isArray()` function is used to detect arrays. **Other Alternatives** Some alternative approaches could be considered: 1. **Iterating through object properties using for...in**: Instead of using `Object.keys()`, you could use a traditional `for...in` loop to iterate through an object's properties. 2. **Using a dedicated library**: There are libraries like Lodash or Ramda that provide optimized functions for comparing objects and arrays. 3. **Using SIMD instructions**: If the benchmark is running on a device with SIMD (Single Instruction, Multiple Data) capabilities, you could use intrinsics to perform operations on multiple elements simultaneously. Keep in mind that these alternatives might not be suitable for this specific benchmark, as they may introduce additional overhead or change the behavior of the code.
Related benchmarks:
lodash deepEqual vs fast-deep-equal v2
Underscore isEqual, fast-deep-equal test
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?