Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
fast-equals
Advanced tools
The fast-equals package is a high-performance deep equality checking library that provides functions to determine if two values are deeply equal. It is optimized for speed and can handle complex data structures, including objects, arrays, dates, and more.
deepEqual
Checks if two values are deeply equal, meaning all their nested properties are equal.
const { deepEqual } = require('fast-equals');
console.log(deepEqual({ foo: 'bar' }, { foo: 'bar' })); // true
shallowEqual
Checks if two values are shallowly equal, meaning their immediate properties are equal without checking nested objects.
const { shallowEqual } = require('fast-equals');
console.log(shallowEqual({ foo: 'bar' }, { foo: 'bar' })); // true
circularDeepEqual
Checks if two values are deeply equal, including when they have circular references.
const { circularDeepEqual } = require('fast-equals');
const objectA = { foo: 'bar' };
objectA.self = objectA;
const objectB = { foo: 'bar' };
objectB.self = objectB;
console.log(circularDeepEqual(objectA, objectB)); // true
circularShallowEqual
Checks if two values are shallowly equal, including when they have circular references.
const { circularShallowEqual } = require('fast-equals');
const arrayA = ['foo', 'bar'];
arrayA.push(arrayA);
const arrayB = ['foo', 'bar'];
arrayB.push(arrayB);
console.log(circularShallowEqual(arrayA, arrayB)); // true
sameValueZeroEqual
Checks if two values are equal according to the SameValueZero comparison algorithm, which treats NaN as equal to NaN and -0 as equal to +0.
const { sameValueZeroEqual } = require('fast-equals');
console.log(sameValueZeroEqual(NaN, NaN)); // true
Lodash's isEqual function is a popular utility for performing deep equality checks. It is part of the larger Lodash library, which offers a wide range of utilities for working with arrays, objects, and other data types. Compared to fast-equals, lodash.isequal may be slower but is part of a well-established utility library with a large user base.
The deep-equal package provides functions for deep equality checking. It is less optimized for performance compared to fast-equals but offers a simple API for checking deep equality between two values.
Ramda is a functional programming library that includes a deep equality checking function called equals. Ramda's approach is more functional and immutable compared to fast-equals, and it is designed to work well in a functional programming context.
Perform blazing fast equality comparisons (either deep or shallow) on two objects passed. It has no dependencies, and is ~1.3kB when minified and gzipped.
Unlike most equality validation libraries, the following types are handled out-of-the-box:
NaN
Date
objectsRegExp
objectsMap
/ Set
iterablesPromise
objectsreact
elementsStarting with version 1.5.0
, circular objects are supported for both deep and shallow equality (see circularDeepEqual
and circularShallowEqual
). You can also create a custom nested comparator, for specific scenarios (see below).
You can either import the full object:
import fe from "fast-equals";
console.log(fe.deep({ foo: "bar" }, { foo: "bar" })); // true
Or the individual imports desired:
import { deepEqual } from "fast-equals";
console.log(deepEqual({ foo: "bar" }, { foo: "bar" })); // true
ESM in browsers:
import fe from "fast-equals";
ESM in NodeJS:
import fe from "fast-equals/mjs";
CommonJS:
const fe = require("fast-equals").default;
Aliased on the default export as fe.deep
Performs a deep equality comparison on the two objects passed and returns a boolean representing the value equivalency of the objects.
import { deepEqual } from "fast-equals";
const objectA = { foo: { bar: "baz" } };
const objectB = { foo: { bar: "baz" } };
console.log(objectA === objectB); // false
console.log(deepEqual(objectA, objectB)); // true
Aliased on the default export as fe.shallow
Performs a shallow equality comparison on the two objects passed and returns a boolean representing the value equivalency of the objects.
import { shallowEqual } from "fast-equals";
const nestedObject = { bar: "baz" };
const objectA = { foo: nestedObject };
const objectB = { foo: nestedObject };
const objectC = { foo: { bar: "baz" } };
console.log(objectA === objectB); // false
console.log(shallowEqual(objectA, objectB)); // true
console.log(shallowEqual(objectA, objectC)); // false
Aliased on the default export as fe.sameValueZero
Performs a SameValueZero
comparison on the two objects passed and returns a boolean representing the value equivalency of the objects. In simple terms, this means either strictly equal or both NaN
.
import { sameValueZeroEqual } from "fast-equals";
const mainObject = { foo: NaN, bar: "baz" };
const objectA = "baz";
const objectB = NaN;
const objectC = { foo: NaN, bar: "baz" };
console.log(sameValueZeroEqual(mainObject.bar, objectA)); // true
console.log(sameValueZeroEqual(mainObject.foo, objectB)); // true
console.log(sameValueZeroEqual(mainObject, objectC)); // false
Aliased on the default export as fe.circularDeep
Performs the same comparison as deepEqual
but supports circular objects. It is slower than deepEqual
, so only use if you know circular objects are present.
function Circular(value) {
this.me = {
deeply: {
nested: {
reference: this
}
},
value
};
}
console.log(circularDeepEqual(new Circular("foo"), new Circular("foo"))); // true
console.log(circularDeepEqual(new Circular("foo"), new Circular("bar"))); // false
Aliased on the default export as fe.circularShallow
Performs the same comparison as shallowequal
but supports circular objects. It is slower than shallowEqual
, so only use if you know circular objects are present.
const array = ["foo"];
array.push(array);
console.log(circularShallowEqual(array, ["foo", array])); // true
console.log(circularShallowEqual(array, [array])); // false
Aliased on the default export as fe.createCustom
Creates a custom equality comparator that will be used on nested values in the object. Unlike deepEqual
and shallowEqual
, this is a partial-application function that will receive the internal comparator and should return a function that compares two objects.
The signature is as follows:
createCustomEqual(deepEqual: function) => (objectA: any, objectB: any, meta: any) => boolean;
The meta
parameter is whatever you want it to be. It will be passed through to all equality checks, and is meant specifically for use with custom equality methods. For example, with the circularDeepEqual
and circularShallowEqual
methods, it is used to pass through a cache of processed objects.
An example for a custom equality comparison that also checks against values in the meta object:
import { createCustomEqual } from "fast-equals";
const isDeepEqualOrFooMatchesMeta = createCustomEqual(deepEqual => {
return (objectA, objectB, meta) => {
return (
objectA.foo === meta ||
objectB.foo === meta ||
deepEqual(objectA, objectB, meta)
);
};
});
const objectA = { foo: "bar" };
const objectB = { foo: "baz" };
const meta = "bar";
console.log(isDeepEqualOrFooMatchesMeta(objectA, objectB)); // true
All benchmarks were performed on an i7 8-core Arch Linux laptop with 16GB of memory using NodeJS version 8.11.1
, and are based on averages of running comparisons based deep equality on the following object types:
String
, Number
, null
, undefined
)Function
Object
Array
Date
RegExp
react
elementsOperations / second | Relative margin of error | |
---|---|---|
fast-equals | 130,726 | 0.79% |
nano-equal | 103,950 | 0.85% |
shallow-equal-fuzzy | 101,535 | 0.60% |
react-fast-compare | 94,724 | 0.82% |
fast-deep-equal | 92,172 | 0.85% |
underscore.isEqual | 63,431 | 0.88% |
fast-equals (circular) | 53,778 | 0.85% |
deep-equal | 29,739 | 0.54% |
lodash.isEqual | 26,201 | 0.70% |
deep-eql | 16,496 | 0.60% |
assert.deepStrictEqual | 1,565 | 1.04% |
Caveats that impact the benchmark (and accuracy of comparison):
nano-equal
does not strictly compare object property structure, array length, or object type, nor SameValueZero
equality for datesshallow-equal-fuzzy
does not strictly compare object type or regexp values, nor SameValueZero
equality for datesfast-deep-equal
does not support NaN
or SameValueZero
equality for datesreact-fast-compare
does not support NaN
or SameValueZero
equality for dates, and does not compare function
equalityunderscore.isEqual
does not support SameValueZero
equality for primitives or datesdeep-equal
does not support NaN
and does not strictly compare object type, or date / regexp values, nor uses SameValueZero
equality for datesdeep-eql
does not support SameValueZero
equality for zero equality (positive and negative zero are not equal)assert.deepStrictEqual
does not support NaN
or SameValueZero
equality for datesAll of these have the potential of inflating the respective library's numbers in comparison to fast-equals
, but it was the closest apples-to-apples comparison I could create of a reasonable sample size. Map
s, Promise
s, and Set
s were excluded from the benchmark entirely because no library other than lodash
supported their comparison. The same logic applies to react
elements (which can be circular objects), but simple elements are non-circular objects so I kept the react
comparison very basic to allow it to be included.
Standard practice, clone the repo and npm i
to get the dependencies. The following npm scripts are available:
clean:dist
, clean:es
, and clean:lib
scriptsrimraf
on the dist
folderrimraf
on the es
folderrimraf
on the lib
folderbuild
and build:minified
scriptssrc
folder (also runs on dev
script)lint
script, but with auto-fixerlint
, test:coverage
, transpile:lib
, transpile:es
, and dist
scriptsdev
test
foldertest
with code coverage calculation via nyc
test
but keep persistent watchersrc
folder (transpiled to es
folder without transpilation of ES2015 export syntax)src
folder (transpiled to lib
folder)1.6.1
babel@7
"sideEffects": false
to package.json
for better tree-shaking in webpack
FAQs
A blazing fast equality comparison, either shallow or deep
The npm package fast-equals receives a total of 2,925,936 weekly downloads. As such, fast-equals popularity was classified as popular.
We found that fast-equals demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.