
Security News
Socket Releases Free Certified Patches for Nuxt Security Vulnerabilities
Socket releases free Certified Patches for high-severity Nuxt vulnerabilities, including server-side remote code execution through server island props.
fast-equals
Advanced tools
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:
react elements and Arguments)ArrayBuffer / TypedArray / DataView instancesDate objectsRegExp objectsMap / Set iterablesPromise objectsnew Boolean() / new Number() / new String())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.
import { deepEqual } from 'fast-equals';
console.log(deepEqual({ foo: 'bar' }, { foo: 'bar' })); // true
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:
fast-equals/dist/esm/index.mjsfast-equals/dist/cjs/index.cjsfast-equals/dist/umd/index.jsfast-equals/dist/min/index.jsIf you are having issues loading a specific build type, please file an issue.
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
MapsMap 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.
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
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
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
Just as with deepEqual, both keys and values are compared for deep equality.
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
Performs the same comparison as deepEqual but performs a strict comparison of the objects. In this includes:
Map / Set objectsconst array = [{ foo: 'bar' }];
const otherArray = [{ foo: 'bar' }];
array.bar = 'baz';
otherArray.bar = 'baz';
console.log(strictDeepEqual(array, otherArray)); // true;
console.log(strictDeepEqual(array, [{ foo: 'bar' }])); // false;
Performs the same comparison as shallowEqual but performs a strict comparison of the objects. In this includes:
Map / Set objectsconst array = ['foo'];
const otherArray = ['foo'];
array.bar = 'baz';
otherArray.bar = 'baz';
console.log(strictDeepEqual(array, otherArray)); // true;
console.log(strictDeepEqual(array, ['foo'])); // false;
Performs the same comparison as circularDeepEqual but performs a strict comparison of the objects. In this includes:
Symbol properties on the objectMap / Set objectsfunction 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)); // true
console.log(circularDeepEqual(first, new Circular('foo'))); // false
Performs the same comparison as circularShallowEqual but performs a strict comparison of the objects. In this
includes:
Map / Set objectsconst array = ['foo'];
const otherArray = ['foo'];
array.push(array);
otherArray.push(otherArray);
array.bar = 'baz';
otherArray.bar = 'baz';
console.log(circularShallowEqual(array, otherArray)); // true
console.log(circularShallowEqual(array, ['foo', array])); // false
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.
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.
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.
RegExp comparatorsmeta in comparison@@toStringTag supportAll 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:
String, Number, null, undefined)FunctionObjectArrayDateRegExpreact elementsTesting 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 comparisonfast-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 datesdeep-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 datesfast-deep-equal does not support NaN or SameValueZero equality for datesnano-equal does not strictly compare object property structure, array length, or object type, nor SameValueZero
equality for datesreact-fast-compare does not support NaN or SameValueZero equality for dates, and does not compare function
equalityshallow-equal-fuzzy does not strictly compare object type or regexp values, nor SameValueZero equality for datesunderscore.isEqual does not support SameValueZero equality for primitives or 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. 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.
Standard practice, clone the repo and npm i to get the dependencies. The following npm scripts are available:
main, module, and browser distributables with rolluprimraf on the dist foldervite playground Appbuildsrc folder (also runs on dev script)lint script, but with auto-fixerlint, test:coverage, transpile:lib, transpile:es, and dist scriptsdevtest foldertest with code coverage calculation via nyctest but keep persistent watcherLodash'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.
FAQs
A blazing-fast equality comparison utility for a variety of use-cases
The npm package fast-equals receives a total of 42,233,311 weekly downloads. As such, fast-equals popularity was classified as popular.
We found that fast-equals demonstrated a healthy version release cadence and project activity because the last version was released less than 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
Socket releases free Certified Patches for high-severity Nuxt vulnerabilities, including server-side remote code execution through server island props.

Security News
An open letter signed by 50 companies, from NVIDIA and Microsoft to Mistral and Hugging Face, urges Washington not to restrict open weight AI.

Security News
/Research
A fake corepack.org site is impersonating the Node.js tool and delivers an infostealer and proxyware to developers who download it.