fast-equals
Perform blazing fast equality comparisons (either deep or shallow) on two objects passed, while also
maintaining a high degree of flexibility for various implementation use-cases. It has no dependencies, and is ~2kB when
minified and gzipped.
The following types are handled out-of-the-box:
- Plain objects (including
react elements and Arguments)
- Arrays
ArrayBuffer / TypedArray / DataView instances
Date objects
RegExp objects
Map / Set iterables
Promise objects
- Primitive wrappers (
new Boolean() / new Number() / new String())
- Custom class instances, including subclasses of native classes
Methods are available for deep, shallow, or referential equality comparison. In addition, you can opt into support for
circular objects, or performing a "strict" comparison with unconventional property definition, or both. You can also
customize any specific type comparison based on your application's use-cases.
Table of contents
Usage
import { deepEqual } from 'fast-equals';
console.log(deepEqual({ foo: 'bar' }, { foo: 'bar' }));
Specific builds
By default, npm should resolve the correct build of the package based on your consumption (ESM vs CommonJS). However, if
you want to force use of a specific build, they can be located here:
- ESM =>
fast-equals/dist/esm/index.mjs
- CommonJS =>
fast-equals/dist/cjs/index.cjs
- UMD =>
fast-equals/dist/umd/index.js
- Minified UMD =>
fast-equals/dist/min/index.js
If you are having issues loading a specific build type,
please file an issue.
Available methods
deepEqual
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);
console.log(deepEqual(objectA, objectB));
Comparing Maps
Map objects support complex keys (objects, Arrays, etc.), however
the spec for key lookups in Map are based on SameZeroValue.
If the spec were followed for comparison, the following would always be false:
const mapA = new Map([[{ foo: 'bar' }, { baz: 'quz' }]]);
const mapB = new Map([[{ foo: 'bar' }, { baz: 'quz' }]]);
deepEqual(mapA, mapB);
To support true deep equality of all contents, fast-equals will perform a deep equality comparison for key and value
parirs. Therefore, the above would be true.
shallowEqual
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);
console.log(shallowEqual(objectA, objectB));
console.log(shallowEqual(objectA, objectC));
sameValueZeroEqual
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));
console.log(sameValueZeroEqual(mainObject.foo, objectB));
console.log(sameValueZeroEqual(mainObject, objectC));
circularDeepEqual
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')));
console.log(circularDeepEqual(new Circular('foo'), new Circular('bar')));
Just as with deepEqual, both keys and values are compared for deep equality.
circularShallowEqual
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]));
console.log(circularShallowEqual(array, [array]));
strictDeepEqual
Performs the same comparison as deepEqual but performs a strict comparison of the objects. In this includes:
- Checking symbol properties
- Checking non-enumerable properties in object comparisons
- Checking full descriptor of properties on the object to match
- Checking non-index properties on arrays
- Checking non-key properties on
Map / Set objects
const array = [{ foo: 'bar' }];
const otherArray = [{ foo: 'bar' }];
array.bar = 'baz';
otherArray.bar = 'baz';
console.log(strictDeepEqual(array, otherArray));
console.log(strictDeepEqual(array, [{ foo: 'bar' }]));
strictShallowEqual
Performs the same comparison as shallowEqual but performs a strict comparison of the objects. In this includes:
- Checking non-enumerable properties in object comparisons
- Checking full descriptor of properties on the object to match
- Checking non-index properties on arrays
- Checking non-key properties on
Map / Set objects
const array = ['foo'];
const otherArray = ['foo'];
array.bar = 'baz';
otherArray.bar = 'baz';
console.log(strictDeepEqual(array, otherArray));
console.log(strictDeepEqual(array, ['foo']));
strictCircularDeepEqual
Performs the same comparison as circularDeepEqual but performs a strict comparison of the objects. In this includes:
- Checking
Symbol properties on the object
- Checking non-enumerable properties in object comparisons
- Checking full descriptor of properties on the object to match
- Checking non-index properties on arrays
- Checking non-key properties on
Map / Set objects
function Circular(value) {
this.me = {
deeply: {
nested: {
reference: this,
},
},
value,
};
}
const first = new Circular('foo');
Object.defineProperty(first, 'bar', {
enumerable: false,
value: 'baz',
});
const second = new Circular('foo');
Object.defineProperty(second, 'bar', {
enumerable: false,
value: 'baz',
});
console.log(circularDeepEqual(first, second));
console.log(circularDeepEqual(first, new Circular('foo')));
strictCircularShallowEqual
Performs the same comparison as circularShallowEqual but performs a strict comparison of the objects. In this
includes:
- Checking non-enumerable properties in object comparisons
- Checking full descriptor of properties on the object to match
- Checking non-index properties on arrays
- Checking non-key properties on
Map / Set objects
const array = ['foo'];
const otherArray = ['foo'];
array.push(array);
otherArray.push(otherArray);
array.bar = 'baz';
otherArray.bar = 'baz';
console.log(circularShallowEqual(array, otherArray));
console.log(circularShallowEqual(array, ['foo', array]));
createCustomEqual
Creates a custom equality comparator that will be used on nested values in the object. Unlike deepEqual and
shallowEqual, this is a factory method that receives the default options used internally, and allows you to override
the defaults as needed. This is generally for extreme edge-cases, or supporting legacy environments.
The signature is as follows:
interface Cache<Key extends object, Value> {
delete(key: Key): boolean;
get(key: Key): Value | undefined;
set(key: Key, value: any): any;
}
interface ComparatorConfig<Meta> {
areArraysEqual: TypeEqualityComparator<any[], Meta>;
areDatesEqual: TypeEqualityComparator<Date, Meta>;
areErrorsEqual: TypeEqualityComparator<Error, Meta>;
areFunctionsEqual: TypeEqualityComparator<(...args: any[]) => any, Meta>;
areMapsEqual: TypeEqualityComparator<Map<any, any>, Meta>;
areObjectsEqual: TypeEqualityComparator<Record<string, any>, Meta>;
arePrimitiveWrappersEqual: TypeEqualityComparator<boolean | string | number, Meta>;
areRegExpsEqual: TypeEqualityComparator<RegExp, Meta>;
areSetsEqual: TypeEqualityComparator<Set<any>, Meta>;
areTypedArraysEqual: TypeEqualityComparator<TypedArray, Meta>;
areUrlsEqual: TypeEqualityComparator<URL, Meta>;
unknownTagComparators: Record<string, TypeEqualityComparator<string, any>>;
}
function createCustomEqual<Meta>(options: {
circular?: boolean;
createCustomConfig?: (defaultConfig: ComparatorConfig<Meta>) => Partial<ComparatorConfig<Meta>>;
createInternalComparator?: (
compare: <A, B>(a: A, b: B, state: State<Meta>) => boolean,
) => (a: any, b: any, indexOrKeyA: any, indexOrKeyB: any, parentA: any, parentB: any, state: State<Meta>) => boolean;
createState?: () => { cache?: Cache; meta?: Meta };
strict?: boolean;
}): <A, B>(a: A, b: B) => boolean;
Create a custom equality comparator. This allows complete control over building a bespoke equality method, in case your
use-case requires a higher degree of performance, legacy environment support, or any other non-standard usage. The
recipes provide examples of use in different use-cases, but if you have a specific goal in mind and would
like assistance feel free to file an issue.
NOTE: Map implementations compare equality for both keys and value. When using a custom comparator and comparing
equality of the keys, the iteration index is provided as both indexOrKeyA and indexOrKeyB to help use-cases where
ordering of keys matters to equality.
unknownTagComparators
If you want to compare objects that have a custom @@toStringTag, you can provide a map of the custom tags you want to
support via the unknownTagComparators option. See this recipe for an example.
Recipes
Some recipes have been created to provide examples of use-cases for createCustomEqual. Even if not directly applicable
to the problem you are solving, they can offer guidance of how to structure your solution.
Benchmarks
All benchmarks were performed on an i9-11900H Ubuntu Linux 24.04 laptop with 64GB of memory using NodeJS version
20.17.0, and are based on averages of running comparisons based deep equality on the following object types:
- Primitives (
String, Number, null, undefined)
Function
Object
Array
Date
RegExp
react elements
- A mixed object with a combination of all the above types
Testing mixed objects equal...
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¬āāāāāāāāāāāāāāāāā
ā Name ā Ops / sec ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¤
ā fast-equals (passed) ā 1416193.769468 ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¤
ā fast-deep-equal (passed) ā 1284824.583215 ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¤
ā react-fast-compare (passed) ā 1246947.505444 ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¤
ā shallow-equal-fuzzy (passed) ā 1238082.379207 ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¤
ā nano-equal (failed) ā 946782.33704 ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¤
ā dequal/lite (passed) ā 758213.632866 ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¤
ā dequal (passed) ā 756789.655029 ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¤
ā fast-equals (circular) (passed) ā 726093.253185 ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¤
ā underscore.isEqual (passed) ā 489748.701783 ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¤
ā assert.deepStrictEqual (passed) ā 453761.890107 ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¤
ā lodash.isEqual (passed) ā 288264.867811 ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¤
ā fast-equals (strict) (passed) ā 217221.619705 ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¤
ā fast-equals (strict circular) (passed) ā 186916.942934 ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¤
ā deep-eql (passed) ā 162487.877883 ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¤
ā deep-equal (passed) ā 916.680714 ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā“āāāāāāāāāāāāāāāāā
Testing mixed objects not equal...
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¬āāāāāāāāāāāāāāāāā
ā Name ā Ops / sec ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¤
ā fast-equals (passed) ā 4687012.640614 ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¤
ā fast-deep-equal (passed) ā 3418170.156109 ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¤
ā react-fast-compare (passed) ā 3283516.669966 ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¤
ā fast-equals (circular) (passed) ā 3268062.099602 ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¤
ā fast-equals (strict) (passed) ā 1747578.66456 ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¤
ā fast-equals (strict circular) (passed) ā 1477873.624956 ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¤
ā dequal/lite (passed) ā 1335397.839502 ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¤
ā dequal (passed) ā 1319426.71146 ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¤
ā shallow-equal-fuzzy (failed) ā 1237432.986615 ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¤
ā nano-equal (passed) ā 1064383.319776 ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¤
ā underscore.isEqual (passed) ā 920462.516736 ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¤
ā lodash.isEqual (passed) ā 379370.998021 ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¤
ā deep-eql (passed) ā 184111.383127 ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¤
ā assert.deepStrictEqual (passed) ā 20775.59065 ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāā¤
ā deep-equal (passed) ā 3678.51009 ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā“āāāāāāāāāāāāāāāāā
Caveats that impact the benchmark (and accuracy of comparison):
Maps, Promises, and Sets were excluded from the benchmark entirely because no library other than deep-eql
fully supported their comparison
fast-deep-equal, react-fast-compare and nano-equal throw on objects with null as prototype
(Object.create(null))
assert.deepStrictEqual does not support NaN or SameValueZero equality for dates
deep-eql does not support SameValueZero equality for zero equality (positive and negative zero are not equal)
deep-equal does not support NaN and does not strictly compare object type, or date / regexp values, nor uses
SameValueZero equality for dates
fast-deep-equal does not support NaN or SameValueZero equality for dates
nano-equal does not strictly compare object property structure, array length, or object type, nor SameValueZero
equality for dates
react-fast-compare does not support NaN or SameValueZero equality for dates, and does not compare function
equality
shallow-equal-fuzzy does not strictly compare object type or regexp values, nor SameValueZero equality for dates
underscore.isEqual does not support SameValueZero equality for primitives or dates
All 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. It should be noted that react
elements can be circular objects, however simple elements are not; I kept the react comparison very basic to allow it
to be included.
Development
Standard practice, clone the repo and npm i to get the dependencies. The following npm scripts are available:
- benchmark => run benchmark tests against other equality libraries
- build => build
main, module, and browser distributables with rollup
- clean => run
rimraf on the dist folder
- dev => start
vite playground App
- dist => run
build
- lint => run ESLint on all files in
src folder (also runs on dev script)
- lint:fix => run
lint script, but with auto-fixer
- prepublish:compile => run
lint, test:coverage, transpile:lib, transpile:es, and dist scripts
- start => run
dev
- test => run AVA with NODE_ENV=test on all files in
test folder
- test:coverage => run same script as
test with code coverage calculation via nyc
- test:watch => run same script as
test but keep persistent watcher