const equalArrays = (arr1, arr2) => arr1.length === arr2.length &&
arr1.every((element, index) => getDifferences(element, arr2[index]) === undefined)
function getDifferences(original, modified) {
if (original instanceof Array && modified instanceof Array) {
return equalArrays(original, modified)
? undefined
: modified
}
if (original instanceof Object && modified instanceof Object) {
let result = {}
for (const key of Object.keys(modified)) {
const diff = getDifferences(original[key], modified[key])
if (diff !== undefined) {
result[key] = diff
}
}
return !Object.keys(result).length
? undefined
: result
}
return original === modified
? undefined
: modified
}
const objA = {
a: 1,
b: 2,
c: {
d: 3,
e: [4, {
h: 5
}]
},
f: [6, 7],
g: 8
}
const objB = {
a: 1,
b: 2,
c: {
d: 3,
e: [4, {
h: 4
}] // field h changed
},
f: [6, 7],
g: 8
}
const arrA = [objA, objB]
const arrB = [objA, objA]
console.log(equalArrays(arrA, arrA))
console.log(equalArrays(arrA, arrB))
const deepEqual = (thing1, thing2) => {
if (thing1 instanceof Object && thing2 instanceof Object)
return Object.keys(thing1).every(key => deepEqual(thing1[key], thing2[key]))
return thing1 === thing2
}
const objA = {
a: 1,
b: 2,
c: {
d: 3,
e: [4, {
h: 5
}]
},
f: [6, 7],
g: 8
}
const objB = {
a: 1,
b: 2,
c: {
d: 3,
e: [4, {
h: 4
}] // field h changed
},
f: [6, 7],
g: 8
}
const arrA = [objA, objB]
const arrB = [objA, objA]
console.log(deepEqual(arrA, arrA))
console.log(deepEqual(arrA, arrB))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
equality by gettingDifferences | |
equality by recursion |
Test name | Executions per second |
---|---|
equality by gettingDifferences | 7233.0 Ops/sec |
equality by recursion | 7396.7 Ops/sec |
Let's break down the benchmark and explain what's being tested.
The provided JSON represents two test cases for JavaScript microbenchmarking on MeasureThat.net. Both tests aim to measure how efficiently browsers can compare equality between two objects or arrays.
Test Case 1: "equality by gettingDifferences"
This test uses a custom function getDifferences
to compare the elements of two arrays or objects. The function recursively checks for differences between corresponding elements, using a similar approach as JSON diffing algorithms. If all elements are equal, it returns an empty object ({}
). Otherwise, it returns the modified array or object.
The test creates two sets of objects: arrA
and arrB
. arrA
consists of two identical objects with nested structures, while arrB
has a single object with the same structure but with a changed field h
.
Test Case 2: "equality by recursion"
This test uses a simpler function deepEqual
to compare two objects. It checks if both objects have the same keys and recursively calls itself for each key-value pair.
Both tests use the same input sets, arrA
and arrB
, and log the results to the console using console.log
.
Library/Function Purpose
In Test Case 1, the getDifferences
function is a custom implementation of a JSON diffing algorithm. It's used to recursively compare objects or arrays and returns an empty object if they're identical.
In Test Case 2, the deepEqual
function is a simple recursive function that checks for equality between two objects by verifying that both have the same keys and values.
Pros and Cons of Different Approaches
Other Considerations
getDifferences
and deepEqual
) introduces some overhead compared to built-in comparison operators.Alternatives
Other approaches for comparing equality between objects or arrays include:
JSON.stringify()
method with custom rejections (e.g., JSON.stringify(obj) === JSON.stringify(otherObj)
).isEqual
function.Keep in mind that these alternatives might have varying performance characteristics and may not be as efficient as the custom implementations used in the benchmark tests.