Fast Equality Comparison Utility

6.0.0 · active · verified Sun Apr 19

fast-equals is a high-performance utility library designed for deep and shallow equality comparisons between JavaScript values, offering significant speed advantages and a small bundle size (~2KB minified and gzipped). Currently stable at version 6.0.0, the library maintains a steady release cadence with minor updates for bug fixes and features, and major versions for breaking changes and significant enhancements. It differentiates itself by providing a comprehensive suite of equality methods (deep, shallow, SameValue, SameValueZero, strict, and circular-aware comparisons), built-in support for a wide array of JavaScript types including Objects, Arrays, TypedArrays, Dates, RegExps, Maps, Sets, Promises, and custom class instances. Furthermore, it offers robust customization options through `createCustomEqual`, allowing developers to define comparison logic for specific types or use cases, all without any external dependencies.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the core `deepEqual`, `shallowEqual`, `strictEqual`, and `createCustomEqual` methods with various data types, illustrating their differing behaviors for object, array, and custom value comparisons.

import { deepEqual, shallowEqual, createCustomEqual, strictEqual } from 'fast-equals';

const objA = { a: 1, b: { c: 2 }, d: [3, 4] };
const objB = { a: 1, b: { c: 2 }, d: [3, 4] };
const objC = { a: 1, b: { c: 99 }, d: [3, 4] };
const arr1 = [1, { foo: 'bar' }, new Date('2023-01-01')];
const arr2 = [1, { foo: 'bar' }, new Date('2023-01-01')];
const arr3 = [1, { foo: 'baz' }, new Date('2023-01-01')];

console.log('Deep equality (objects):', deepEqual(objA, objB)); // true
console.log('Deep equality (objects with difference):', deepEqual(objA, objC)); // false
console.log('Shallow equality (objects):', shallowEqual(objA, objB)); // false (nested objects/arrays are different references)
console.log('Deep equality (arrays):', deepEqual(arr1, arr2)); // true
console.log('Deep equality (arrays with difference):', deepEqual(arr1, arr3)); // false

const customEqual = createCustomEqual({ createComparator: () => (a, b) => a === b || a.value === b.value });
console.log('Custom equality (simple values):', customEqual({ value: 1 }, { value: 1 })); // true

console.log('Strict equality (same value, different reference):', strictEqual([1], [1])); // false
console.log('Strict equality (same reference):', strictEqual(objA, objA)); // true

view raw JSON →