deepkoi
  • { l.name }

v0.1.1, zero dependencies, 4.9 kB gzip

Deep equality for everything structuredClone can copy.

deepkoi compares Maps with object keys, Sets, typed arrays, errors, cycles and shared references the way the platform copies them, and points at the exact spot where two values part ways.

npm i deepkoi
equal(value, structuredClone(value))
true
  • Runs on
  • Node.js 18+
  • Bun
  • Deno
  • Chrome
  • Firefox
  • Safari
  • TypeScript

Features

Everything a copy keeps, nothing it drops

  1. 01

    Clone true

    equal(x, structuredClone(x)) holds for every cloneable value, including class instances and custom errors.

    • 02

      Maps and Sets, deeply

      Object keys and members are paired by structure. The quadratic search only runs on the leftovers.

    • 03

      Bytes are bytes

      Typed arrays, DataView and ArrayBuffer compare their visible bytes, so NaN payloads and -0 count.

    • 04

      Cycles and sharing

      Circular graphs always finish. Strict mode also checks that shared references are arranged the same way.

    • 05

      Errors that matter

      Name, message and cause, with names normalized exactly like structuredClone does.

      equal(
        new TypeError('dry', { cause: 1 }),
        new TypeError('dry', { cause: 2 }),
      ) // false
    • 06

      diff for test runners

      The first difference as a path such as $.users[1].tags, with a reason and both values.

    • 07

      Small and dependency free

      4.9 kB gzip, ESM and CommonJS, hand written types, tree shakeable named exports.

      4.9 kB

    Try it live

    Playground

    Two values, a live verdict. Type JavaScript literals or insert a helper; the real library runs in your browser.

    Examples
    • { p.name }
    Mode

    Left value

    Right value

    Insert into the focused editor

    Helpers: cycle(obj, key) makes a self reference, ref(id, value) and ref(id) share one object, bytes(1, 2) builds a Uint8Array.

    Path to the first difference
    1. { c.label }

    Side by side
    equal
    strictEqual
    JSON.stringify comparison
    Options
    { o.label }

    Why deepkoi

    Why another deep equal?

    Most deep equality helpers were written for JSON shaped data. Give them a Map with object keys, a Float64Array that holds NaN, an Error with a cause or a graph that points back at itself, and their answers start to disagree. Some never return.

    deepkoi takes its rules from structuredClone. If a copy keeps a detail, deepkoi compares it. If a copy drops it, loose mode ignores it too. strictEqual adds the details a copy cannot tell apart: prototypes, the sign of zero, array holes and the way references are shared.

    Answers on cases where libraries disagree (verified with each package)
    CaseExpecteddeepkoinode:utildequalfast-deep-equallodash.isequal
    Map, object keys in another orderequalequalequalequalnot equalequal
    Float64Array [NaN] vs [NaN]equalequalequalnot equalnot equalequal
    DataView, different bytesnot equalnot equalnot equalnot equalhangsnot equal
    Error, different causenot equalnot equalnot equalequalequalequal
    Circular, equalequalequalequalstack overflowstack overflowequal
    Two different WeakMapsnot equalnot equalnot equalequalequalnot equal
    Class instance vs its structuredCloneequalequalnot equalnot equalnot equalnot equal

    Expected answers follow deepkoi loose mode. The last row is a deliberate choice: node:util compares prototypes there, like deepkoi strictEqual.

    Reference

    API

    Four named exports. Each one accepts the same options object.

    equal(a, b, options?): boolean
    Loose deep equality: true whenever one value could be a structuredClone copy of the other.
    strictEqual(a, b, options?): boolean
    Every strict flag on: prototypes, signed zero, symbol keys, holes, sharing shape, extra properties on arrays and errors.
    diff(a, b, options?): Difference | undefined
    The first difference, or undefined. Returns path, reason, both sides and a one line message.
    formatPath(path): string
    Turns a path into an accessor chain such as $.cache.get("key").value.
    import { equal, strictEqual, diff } from 'deepkoi';
    
    const a = { tags: new Set(['koi']), at: new Date(0), bytes: new Uint8Array([1, 2]) };
    const b = structuredClone(a);
    
    equal(a, b);        // true
    strictEqual(a, b);  // true
    
    b.bytes[1] = 3;
    diff(a, b)?.message; // '$.bytes[1]: 2 differs from 3'
    const { equal, formatPath, diff } = require('deepkoi');
    
    const cache = new Map([[{ user: 1 }, { hits: 2 }]]);
    equal(cache, new Map([[{ user: 1 }, { hits: 2 }]])); // true
    
    const d = diff([{ a: 1 }], [{ a: 2 }]);
    formatPath(d.path); // '$[0].a'
    import test from 'node:test';
    import assert from 'node:assert/strict';
    import { diff } from 'deepkoi';
    
    test('render keeps the model', () => {
      const d = diff(render(model), expected);
      assert.equal(d, undefined, d?.message);
    });
    equal(0, -0);                          // true
    equal(0, -0, { signedZero: true });    // false
    strictEqual(0, -0, { signedZero: false }); // true
    
    const s = { v: 1 };
    equal([s, s], [{ v: 1 }, { v: 1 }]);       // true
    strictEqual([s, s], [{ v: 1 }, { v: 1 }]); // false (topology)
    
    equal(/a/g, Object.assign(/a/g, { lastIndex: 2 }), { lastIndex: true }); // false
    Behavior at a glance
    Valueequal (loose)strictEqual
    +0 and -0equaldifferent
    Class instance vs plain objectcompared by own keysprototypes must match
    [ , 1] vs [undefined, 1]equaldifferent
    [s, s] vs two equal copiesequaldifferent
    Error namenormalized like structuredCloneexact, plus own properties
    Functions, Promise, WeakMapby referenceby reference

    How to

    Deep compare values in JavaScript

    Short answers to the questions people search for, each with code you can paste into Node, Bun, Deno or the browser.

    How to deep compare two objects

    Call equal(a, b). It walks both values and compares own keys and values, in any key order. A missing key and a key set to undefined are different.

    import { equal } from 'deepkoi';
    
    equal({ a: [1, { b: 2 }] }, { a: [1, { b: 2 }] });
    // true
    equal({ a: 1 }, { a: 1, b: undefined });
    // false: b is missing on the left

    How to compare Maps and Sets deeply

    Order does not matter, and object keys are matched by structure, not by reference. Plain JSON based checks cannot do this.

    equal(
      new Map([[{ id: 1 }, 'a'], [{ id: 2 }, 'b']]),
      new Map([[{ id: 2 }, 'b'], [{ id: 1 }, 'a']]),
    ); // true
    
    equal(new Set([[1], [2]]), new Set([[2], [1]]));
    // true

    How to find where two objects differ

    diff(a, b) returns the first difference as a path with a reason, which makes failing tests easy to read.

    import { diff, formatPath } from 'deepkoi';
    
    const d = diff(
      { users: [{ tags: ['a'] }] },
      { users: [{ tags: ['b'] }] },
    );
    formatPath(d.path); // '$.users[0].tags[0]'
    d.reason;           // 'value'

    How to check that structuredClone kept everything

    equal(x, structuredClone(x)) is true for every value the clone accepts, including Dates, typed arrays and circular graphs.

    const state = { at: new Date(), seen: new Set([1]) };
    state.self = state;
    
    equal(state, structuredClone(state)); // true

    Performance

    Benchmarks

    Operations per second, higher is better. Median of 7 runs, measured on one machine; your numbers will differ.

    dequal 2.0.34.32Mfast-deep-equal/es6 3.1.33.58Mdeepkoi equal2.73Mnode:util isDeepStrictEqual1.63Mlodash.isequal 4.5.0999.3kdeepkoi strictEqual671.5k
    dequal 2.0.33.9kfast-deep-equal/es6 3.1.33.3kdeepkoi equal3.0knode:util isDeepStrictEqual1.6klodash.isequal 4.5.01.0kdeepkoi strictEqual663
    dequal 2.0.3178.3kfast-deep-equal/es6 3.1.3107.0kdeepkoi equal67.7knode:util isDeepStrictEqual43.5kdeepkoi strictEqual34.0klodash.isequal 4.5.08.3k
    node:util isDeepStrictEqual463.7kdeepkoi equal427.6kdeepkoi strictEqual386.4kfast-deep-equal/es6 3.1.3182.1klodash.isequal 4.5.017.6kdequal 2.0.35.3k
    dequal 2.0.37.0kdeepkoi equal6.6kfast-deep-equal/es6 3.1.35.8knode:util isDeepStrictEqual2.7klodash.isequal 4.5.01.9kdeepkoi strictEqual1.4k

    AMD Ryzen 7 7800X3D, Windows 10, Node v24.16.0, ops/s, median of 7 runs

    Plain object libraries that skip Maps, cycles or byte checks are faster on simple data. deepkoi loose mode beats node:util on most cases here, by 1.6 to 1.9 times; strictEqual pays for tracking shared references.

    gzip, comments stripped
    4.9 kB
    runtime dependencies
    0
    tests
    84
    line coverage
    100%

    FAQ

    Questions

    Loose mode follows structuredClone, and a copy of a class instance is a plain object. Use strictEqual, or pass { prototypes: true }, when the class matters.

    In the first array both slots hold the same object, so changing one changes the other. structuredClone keeps that sharing, and strictEqual checks it. Pass { topology: false } to turn it off.

    Yes. Cycles of any length finish, in both modes and in diff. Loose mode compares the unrolled trees; strict mode also requires the same shape.

    It runs everywhere, adds diff, and follows structuredClone: extra properties on Map, Set, Date or typed arrays are ignored, and lastIndex is opt in. The README lists every difference, and a test checks each one.

    There is nothing inside them that can be compared reliably, and structuredClone refuses them too. Two different functions are never equal; the same function always is.

    Install deepkoi, which has zero runtime dependencies, and call equal(a, b). In Node you can also use util.isDeepStrictEqual; the README lists every case where the two answer differently.

    Yes. It is plain JavaScript with no Node APIs, published as ESM with a CommonJS build, so it runs in browsers, Node 18 and later, Bun and Deno.

    Compare values the way the platform copies them.

    One install, zero dependencies, types included. Works the same in Node, Bun, Deno and the browser.