Skip to content

equal()

Eugene Lazutkin edited this page Nov 9, 2020 · 2 revisions

This function compares two JavaScript objects for deep equivalency. It can handle objects with circular dependencies.

Introduction

import equal from 'deep6';
// or: import {equal} from 'deep6';

equal({a: [1]}, {a: [1]}); // true
equal({a: [1]}, {a: [2]}); // false
equal({a: [1]}, {b: [1]}); // false

API

equal(a, b [, options])

Arguments:

  • a — a required argument. It can be anything.

  • b — a required argument. It can be anything.

  • options — an optional object. The following optional properties are recognized:

    • circular — a boolean flag. When true, special algorithms are used to detect internal loops in objects.
      • It is totally safe to turn it on for all objects, but checks will require more memory and more CPU. In some cases, for performance reasons, you may want to turn it off.
    • symbols — a boolean flag. When true, symbol properties would be checked for equivalency as well.
      • Normally symbol properties are hidden from enumerating using classical means.
    • ignoreFunctions — a boolean flag. When true, if both properties are functions, they are assumed to be equivalent automatically.
    • loose — a boolean flag. When true, primitives are compared with == rather than ===.
    • Any other properties documented in unify().

    The default: {circular: true}.

The function returns a boolean value, which indicates the equivalence of its first two arguments.

Notes

Implemented using unify():

const defaultOptions = {circular: true};
const equal = (a, b, options = defaultOptions) => !!unify(a, b, null, options);
Clone this wiki locally