fast-equals
A blazing fast equality comparison, either shallow or deep
Last updated 4 years ago by planttheidea .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install fast-equals 
SYNC missed versions from official npm registry.

fast-equals

Perform blazing fast equality comparisons (either deep or shallow) on two objects passed. It has no dependencies, and is ~1.23kB when minified and gzipped.

Unlike most equality validation libraries, the following types are handled out-of-the-box:

  • NaN
  • Date objects
  • RegExp objects
  • Map / Set iterables
  • Promise objects
  • react elements

Starting 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).

Table of contents

Usage

import { deepEqual } from 'fast-equals';

console.log(deepEqual({ foo: 'bar' }, { foo: 'bar' })); // true

Specific builds

There are three builds, an ESM build for modern build systems / runtimes, a CommonJS build for traditional NodeJS environments, and a UMD build for legacy implementations. The ideal one will likely be chosen for you automatically, however if you want to use a specific build you can always import it directly:

  • ESM => fast-equals/dist/fast-equals.esm.js
    • For older nodejs versions that do not allow ESM with file extensions other than .mjs => fast-equals/dist/fast-equals.mjs
  • CommonJS => fast-equals/dist/fast-equals.cjs.js
  • UMD => fast-equals/dist/fast-equals.js
  • Minified UMD => fast-equals/dist/fast-equals.min.js

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); // false
console.log(deepEqual(objectA, objectB)); // true

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); // false
console.log(shallowEqual(objectA, objectB)); // true
console.log(shallowEqual(objectA, objectC)); // false

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)); // true
console.log(sameValueZeroEqual(mainObject.foo, objectB)); // true
console.log(sameValueZeroEqual(mainObject, objectC)); // false

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'))); // true
console.log(circularDeepEqual(new Circular('foo'), new Circular('bar'))); // false

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])); // true
console.log(circularShallowEqual(array, [array])); // false

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:

type InternalEqualityComparator = (
  a: any,
  b: any,
  indexOrKeyA: any,
  indexOrKeyB: any,
  parentA: any,
  parentB: any,
  meta: any,
) => boolean;

type TypeEqualityComparator = <Type, Meta>(
  a: Type,
  b: Type,
  isEqual: InternalEqualityComparator,
  meta: Meta,
) => boolean;

interface CreateComparatorCreatorOptions<Meta> {
  areArraysEqual: TypeEqualityComparator<any[], Meta>;
  areDatesEqual: TypeEqualityComparator<Date, Meta>;
  areMapsEqual: TypeEqualityComparator<Map<any, any>, Meta>;
  areObjectsEqual: TypeEqualityComparator<Record<string, any>, Meta>;
  areRegExpsEqual: TypeEqualityComparator<RegExp, Meta>;
  areSetsEqual: TypeEqualityComparator<Set<any>, Meta>;
  createIsNestedEqual?: (
    comparator: <A, B>(a: A, b: B, meta: Meta) => boolean,
  ) => InternalEqualityComparator;
}

function createCustomEqual(
  getComparatorOptions: (
    defaultOptions: CreateComparatorOptions,
  ) => Partial<CreateComparatorOptions>,
): EqualityComparator;

The meta parameter above 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. You also only need to return the override handlers; absent being provided, the defaults are used.

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.

Legacy environment support

Starting in 4.x.x, RegExp.prototype.flags is expected to be available in the environment. All modern browsers support this feature, however there may be situations where a legacy environmental support is required (example: IE11). If you need to support such an environment, creating a custom comparator that uses a more verbose comparison of all possible flags is a simple solution.

import { createCustomEqual, sameValueZeroEqual } from 'deep-Equals';

function areRegExpsEqual(a: RegExp, b: RegExp): Boolean {
  return (
    a.source === b.source &&
    a.global === b.global &&
    a.ignoreCase === b.ignoreCase &&
    a.multiline === b.multiline &&
    a.unicode === b.unicode &&
    a.sticky === b.sticky &&
    a.lastIndex === b.lastIndex
  );
}

const deepEqual = createCustomEqual(() => ({ areRegExpEqual }));
const shallowEqual = createCustomEqual(() => ({
  areRegExpsEqual,
  createIsNestedEqual: () => sameValueZeroEqual,
}));

Custom targeted comparisons

Sometimes it is necessary to squeeze every once of performance out of your runtime code, and deep equality checks can be a bottleneck. When this is occurs, it can be advantageous to build a custom comparison that allows for highly specific equality checks.

An example where you know the shape of the objects being passed in, where the foo property is a simple primitive and the bar property is a nested object:

import { createCustomEqual } from 'fast-equals';

const isCollectionEqual = createCustomEqual<Meta>({
  areObjectsEqual(a, b, isEqual, meta) {
    return a.foo === b.foo && isEqual(a.bar, b.bar, meta);
  },
});

This avoids ambiguous iteration and type-checking, which can boost performance in extreme hot-path scenarios.

Here is another example, with a custom equality comparison that also checks against values in the meta object:

import { createCustomEqual } from 'fast-equals';

const isDeepEqualOrFooMatchesMeta = createCustomEqual<Meta>(() => ({
  createIsNestedEqual(deepEqual) {
    return (a, b, keyA, keyB, parentA, parentB, meta) =>
      a === meta || b === meta || deepEqual(a, b, meta);
  },
}));

console.log(
  'shallow',
  isDeepEqualOrFooMatchesMeta({ foo: 'bar' }, { foo: 'baz' }, 'bar'),
);
console.log(
  'deep',
  isDeepEqualOrFooMatchesMeta(
    { nested: { foo: 'bar' } },
    { nested: { foo: 'baz' } },
    'bar',
  ),
); // true

createCustomCircularEqual

Operates nearly identically to createCustomEqual, with the difference being that the meta property expected by the comparator is expected to have a WeakMap contract. This is because it is used for caching accessed objects to avoid maximum stack exceeded errors. The most common use for this method is a simple way to support circular checks for references that do not have WeakMap natively.

Legacy environment support

Starting in 4.x.x, WeakMap is expected to be available in the environment. All modern browsers support this global object, however there may be situations where a legacy environmental support is required (example: IE11). If you need to support such an environment, creating a custom comparator that uses a custom cache implementation with the same contract is a simple solution.

import { createCustomEqual, sameValueZeroEqual } from 'deep-Equals';

interface Cache {
  delete(key: object): boolean;
  get(key: object): any;
  set(key: object, value: any);

  customMethod(): void;
  customValue: string;
}

function getCache(): Cache {
  const entries = [];

  return {
    delete(key) {
      for (let index = 0; index < entries.length; ++index) {
        if (entries[index][0] === key) {
          entries.splice(index, 1);
          return true;
        }
      }

      return false;
    },

    get(key) {
      for (let index = 0; index < entries.length; ++index) {
        if (entries[index][0] === key) {
          return entries[index][1];
        }
      }
    },

    set(key, value) {
      for (let index = 0; index < entries.length; ++index) {
        if (entries[index][0] === key) {
          entries[index][1] = value;
          return this;
        }
      }

      entries.push([key, value]);

      return this;
    },

    customMethod() {
      console.log('hello!');
    },
    customValue: 'goodbye',
  };
}

const customCircularHandler = createCustomCircularEqual(() => ({}));

const circularDeepEqual = (a, b) => customCircularHandler(a, b, getCache());

Benchmarks

All benchmarks were performed on an i7-8650U Ubuntu Linux laptop with 24GB of memory using NodeJS version 12.19.1, 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
Operations / second
fast-equals 153,880
fast-deep-equal 144,035
react-fast-compare 130,324
nano-equal 104,624
fast-equals (circular) 97,610
shallow-equal-fuzzy 83,946
underscore.isEqual 47,370
lodash.isEqual 25,053
deep-eql 22,146
assert.deepStrictEqual 532
deep-equal 209

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
  • 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 webpack 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

Current Tags

  • 6.2.2                                ...           latest (a month ago)
  • 5.4.5                                ...           latest-v5 (a month ago)
  • 6.0.0-rc.1                                ...           next (a year ago)

87 Versions

  • 6.2.2                                ...           a month ago
  • 6.2.1                                ...           a month ago
  • 5.4.5                                ...           a month ago
  • 6.2.0                                ...           2 months ago
  • 6.1.1                                ...           2 months ago
  • 5.4.4                                ...           2 months ago
  • 6.1.0                                ...           2 months ago
  • 5.4.3                                ...           2 months ago
  • 6.0.4                                ...           2 months ago
  • 5.4.2                                ...           3 months ago
  • 6.0.3                                ...           3 months ago
  • 5.4.1                                ...           5 months ago
  • 6.0.2                                ...           5 months ago
  • 6.0.1                                ...           5 months ago
  • 6.0.0                                ...           a year ago
  • 6.0.0-rc.1                                ...           a year ago
  • 6.0.0-rc.0                                ...           a year ago
  • 6.0.0-beta.1                                ...           a year ago
  • 6.0.0-beta.0                                ...           a year ago
  • 5.4.0                                ...           a year ago
  • 5.3.4                                ...           a year ago
  • 5.3.3                                ...           a year ago
  • 5.3.3-beta.1                                ...           a year ago
  • 5.3.3-beta.0                                ...           a year ago
  • 5.2.3-beta.1                                ...           a year ago
  • 5.3.2                                ...           a year ago
  • 5.3.1                                ...           a year ago
  • 5.3.0 [deprecated]           ...           a year ago
  • 5.2.3-beta.0                                ...           a year ago
  • 5.2.2                                ...           2 years ago
  • 5.2.2-beta.0                                ...           2 years ago
  • 5.2.1                                ...           2 years ago
  • 5.2.1-beta.0                                ...           2 years ago
  • 5.2.0                                ...           2 years ago
  • 5.1.3                                ...           2 years ago
  • 5.1.2                                ...           2 years ago
  • 5.1.1                                ...           2 years ago
  • 5.1.0                                ...           2 years ago
  • 5.0.1                                ...           4 years ago
  • 5.0.0                                ...           4 years ago
  • 5.0.0-beta.6                                ...           4 years ago
  • 5.0.0-beta.5                                ...           4 years ago
  • 5.0.0-beta.4                                ...           4 years ago
  • 5.0.0-beta.3                                ...           4 years ago
  • 5.0.0-beta.2                                ...           4 years ago
  • 5.0.0-beta.1                                ...           4 years ago
  • 5.0.0-beta.0                                ...           4 years ago
  • 4.0.3                                ...           4 years ago
  • 4.0.2                                ...           4 years ago
  • 4.0.1                                ...           4 years ago
  • 4.0.1-beta.3                                ...           4 years ago
  • 4.0.1-beta.2                                ...           4 years ago
  • 4.0.1-beta.1                                ...           4 years ago
  • 4.0.1-beta.0                                ...           4 years ago
  • 4.0.0                                ...           4 years ago
  • 4.0.0-beta.0                                ...           4 years ago
  • 3.0.3                                ...           5 years ago
  • 3.0.2                                ...           5 years ago
  • 3.0.1                                ...           5 years ago
  • 3.0.0                                ...           5 years ago
  • 2.0.4                                ...           5 years ago
  • 2.0.3                                ...           6 years ago
  • 2.0.2                                ...           6 years ago
  • 2.0.1                                ...           6 years ago
  • 2.0.0                                ...           8 years ago
  • 1.6.3                                ...           8 years ago
  • 1.6.2                                ...           8 years ago
  • 1.6.1                                ...           8 years ago
  • 1.6.0                                ...           8 years ago
  • 1.5.3                                ...           9 years ago
  • 1.5.2                                ...           9 years ago
  • 1.5.1                                ...           9 years ago
  • 1.5.0                                ...           9 years ago
  • 1.4.1                                ...           9 years ago
  • 1.4.0                                ...           9 years ago
  • 1.3.1                                ...           9 years ago
  • 1.3.0                                ...           9 years ago
  • 1.2.1                                ...           9 years ago
  • 1.2.0                                ...           9 years ago
  • 1.1.0                                ...           9 years ago
  • 1.0.6                                ...           9 years ago
  • 1.0.5                                ...           9 years ago
  • 1.0.4                                ...           9 years ago
  • 1.0.3                                ...           9 years ago
  • 1.0.2                                ...           9 years ago
  • 1.0.1                                ...           9 years ago
  • 1.0.0                                ...           9 years ago
Maintainers (1)
Downloads
Total 208
Today 0
This Week 25
This Month 130
Last Day 2
Last Week 80
Last Month 2
Dependencies (0)
None
Dev Dependencies (42)

© 2010 - cnpmjs.org x YWFE | Home | YWFE