<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
function is(x, y) {
return (
(x === y && (x !== 0 || 1 / x === 1 / y)) || (x !== x && y !== y) // eslint-disable-line no-self-compare
);
}
const hasOwnProperty = Object.prototype.hasOwnProperty;
function shallowEqual(objA, objB) {
if (is(objA, objB)) {
return true;
}
if (
typeof objA !== 'object' ||
objA === null ||
typeof objB !== 'object' ||
objB === null
) {
return false;
}
const keysA = Object.keys(objA);
const keysB = Object.keys(objB);
if (keysA.length !== keysB.length) {
return false;
}
// Test for A's keys different from B.
for (let i = 0; i < keysA.length; i++) {
const currentKey = keysA[i];
if (
!hasOwnProperty.call(objB, currentKey) ||
!is(objA[currentKey], objB[currentKey])
) {
return false;
}
}
return true;
}
var data = [{
foo: {
bar: {
foo: {
bar: ''
}
}
}
}, {}, {}];
var result = R.equals(data, [{foo: {bar: {foo: {bar: ''}}}}, {}, {}]);
var result = shallowEqual(data, [{foo: {bar: {foo: {bar: ''}}}}, {}, {}]);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Ramda | |
shallowEqual |
Test name | Executions per second |
---|---|
Ramda | 190091.5 Ops/sec |
shallowEqual | 1477757.4 Ops/sec |
I'll break down the benchmark definition and explain what's being tested, compared, and their pros and cons.
Benchmark Definition
The benchmark measures the performance of two JavaScript functions: shallowEqual
(custom implementation) and Ramda's equals
function. Both functions compare two objects for shallow equality.
What is tested?
shallowEqual
: a custom JavaScript function implemented by the test user, which checks if two objects are equal by comparing their keys and values.equals
: a functional programming library function that compares two values for equality.Options compared
The benchmark compares the performance of the following options:
equals
: A widely-used functional programming library function that compares two values for equality.Pros and Cons
equals
:Library: Ramda
Ramda is a popular JavaScript functional programming library that provides a wide range of functions for working with data. The equals
function is one of its most commonly used functions, and it's designed to compare two values for equality.
Special JS feature or syntax: None
This benchmark does not use any special JavaScript features or syntax, such as ES6 classes, async/await, or Promises.
Other alternatives
If you're looking for alternative libraries or implementations for shallow equality checks, some options include:
isEqual
function.deepEqual
method ( deprecated in favor of Lodash's implementation)JSON.stringify()
and JSON.parse()
methods.Keep in mind that the best approach depends on your specific use case and requirements. If you need a lightweight, high-performance solution for shallow equality checks, Ramda's equals
function may be a good choice. However, if you require more advanced features or customization options, a custom implementation or another library like Lodash may be a better fit.