Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
equality objects functions
(version: 0)
Comparing performance of:
equality by gettingDifferences vs equality by recursion
Created:
5 years ago
by:
Registered User
Jump to the latest result
Tests:
equality by gettingDifferences
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))
equality by recursion
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))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
equality by gettingDifferences
equality by recursion
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 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** 1. **getDifferences (JSON diffing)**: * Pros: Can handle nested structures with different data types. * Cons: Can be slower due to the complexity of the recursive algorithm. 2. **deepEqual (recursive function)**: * Pros: Simple, straightforward implementation, and relatively fast. * Cons: May not perform well for large or deeply nested objects. **Other Considerations** * The tests only compare equality between identical objects or arrays with minimal changes (e.g., a single field changed). * The use of custom functions (`getDifferences` and `deepEqual`) introduces some overhead compared to built-in comparison operators. * The benchmark measures the number of executions per second, which can be influenced by various factors such as CPU, memory, and JavaScript engine optimizations. **Alternatives** Other approaches for comparing equality between objects or arrays include: 1. Using the built-in `JSON.stringify()` method with custom rejections (e.g., `JSON.stringify(obj) === JSON.stringify(otherObj)`). 2. Utilizing libraries like Lodash's `isEqual` function. 3. Implementing a custom comparison function using a library like Underscore.js. 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.
Related benchmarks:
Spread vs Object.assign (modify ) vs Object.assign (new)
Spread vs Assign benchmark2
Spread vs Assign benchmark 2
Object.assign() vs spread operator (New object)
type of object
Comments
Confirm delete:
Do you really want to delete benchmark?