Socket
Socket
Sign inDemoInstall

fast-equals

Package Overview
Dependencies
0
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    fast-equals

A blazing fast equality comparison, either shallow or deep


Version published
Weekly downloads
2.8M
increased by3.3%
Maintainers
1
Install size
46.8 kB
Created
Weekly downloads
 

Package description

What is fast-equals?

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.

What are fast-equals's main functionalities?

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

Other packages similar to fast-equals

Changelog

Source

1.1.0

  • Add TypeScript typings (thanks @josh-sachs)

Readme

Source

fast-equals

Perform blazing fast equality comparisons (either deep or shallow) on two objects passed. It has no dependencies, and is ~1.1Kb 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

You can also create a custom nested comparator, for specific scenarios (see below).

Table of contents

Usage

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

Available methods

deepEqual

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
shallowEqual

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
createCustomEqual

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.

A common use case for this is to handle circular objects (which fast-equals does not handle by default). Example:

import {createCustomEqual} from 'fast-equals';
import decircularize from 'decircularize';

const isDeepEqualCircular = createCustomEqual((comparator) => {
  return (objectA, objectB) => {
    return comparator(decircularize(objectA), decircularize(objectB));
  };
});

const objectA = {};
const objectB = {};

objectA.a = objectA;
objectA.b = objectB;

objectB.a = objectA;
objectB.b = objectB;

console.log(isDeepEqualCircular(objectA, objectB)); // true

Benchmarks

All benchmarks are based on averages of running comparisons based on the following data types:

  • Primitives (String, Number, null, undefined)
  • Functions
  • Objects
  • Arrays
  • Dates
  • RegExps
  • A mixed object with a combination of all the above types
Operations / secondRelative margin of error
fast-equals197,6150.53%
nano-equal121.8430.49%
fast-deep-equal102,2570.41%
shallow-equal-fuzzy80,2240.55%
underscore.isEqual51,1100.52%
deep-equal32,9070.82%
lodash.isEqual26,0210.48%
deep-eql14,5890.66%
assert.deepStrictEqual4411.39%

Caveats that impact the benchmark:

  • 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
  • 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
  • deep-equal does not support NaN and does not strictly compare object type, or date / regexp values, nor uses SameValueZero equality for dates
  • deep-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 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. Maps and Sets were excluded from the benchmark entirely because no library other than lodash supported their comparison.

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 unminified dist version with source map and NODE_ENV=development via webpack
  • build:minified => build minified dist version with NODE_ENV=production via webpack
  • clean => run clean:dist, clean:es, and clean:lib scripts
  • clean:dist => run rimraf on the dist folder
  • clean:es => run rimraf on the es folder
  • clean:lib => run rimraf on the lib folder
  • dev => start webpack playground App
  • dist => run build and build:minified scripts
  • lint => run ESLint on all files in src folder (also runs on dev script)
  • lint:fix => run lint script, but with auto-fixer
  • prepublish =>
  • 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
  • transpile:es => run Babel on all files in src folder (transpiled to es folder without transpilation of ES2015 export syntax)
  • transpile:lib => run Babel on all files in src folder (transpiled to lib folder)

Keywords

FAQs

Last updated on 23 Feb 2018

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc