npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@azuliani/tree-diff

v0.0.3

Published

Tree/trie-based JSON-friendly diff and strict patcher.

Readme

tree-diff

Tree/trie-based JSON-friendly diff + strict patcher for plain objects and arrays.

  • Deltas are compact (path-compressed trie) and can be JSON.stringify()’d directly.
  • apply() is strict: it refuses to patch if the target doesn’t match the expected shape.
  • Preserves Date and undefined across JSON using per-leaf metadata.

Status: draft (v0.1).

Install

npm i @azuliani/tree-diff

Node: >=23.6.0 (see package.json engines).

Quick start

import { apply, diff } from "@azuliani/tree-diff";

const lhs = { name: "Alice", createdAt: new Date("2026-02-04T00:00:00.000Z") };
const rhs = { name: "Bob", createdAt: new Date("2026-02-05T00:00:00.000Z"), extra: undefined };

// `diff()` returns an empty array when there are no changes.
const delta = diff(lhs, rhs);

// Deltas are JSON-safe.
const wire = JSON.stringify(delta);
const parsed = JSON.parse(wire);

const target = structuredClone(lhs);
apply(target, parsed);

// target is now equal to rhs (including Date + explicit undefined).

CommonJS:

const { apply, diff } = require("@azuliani/tree-diff");

API

diff(lhs, rhs) -> TreeDelta

Computes a delta that transforms lhs into rhs.

  • Roots must both be containers of the same kind (both arrays or both plain objects), otherwise throws TreeDiffError("INVALID_ROOT").
  • Returns an empty array when there are no changes.
  • Throws on cycles (CYCLE_DETECTED) or unsupported values (UNSUPPORTED_TYPE).

apply(target, delta) -> object | unknown[]

Mutates target in place and returns it (for chaining convenience).

  • Strict: throws if keys/indices don’t exist or preconditions don’t match (TYPE_MISMATCH, PRECONDITION_FAILED).
  • Uses meta to restore Date and undefined in leaf payloads.

Delta format (wire)

The delta is a list of “entries”, where each entry is either:

  • a leaf (new/edit/delete), or
  • a node (a path-compressed trie node that groups shared prefixes).
type Key = string | number;
type RelPath = Key[];

type Meta = {
  d?: RelPath[]; // Date paths (relative to leaf rhs)
  u?: RelPath[]; // undefined paths (relative to leaf rhs)
};

type Leaf =
  | [key: Key, kind: "D"]
  | [key: Key, kind: "N" | "E", rhs: unknown, meta?: Meta];

type Node = [path: Key[], entries: Entry[]];
type Entry = Leaf | Node;
type TreeDelta = Entry[];

Supported values and constraints

Supported runtime values in lhs/rhs:

  • JSON primitives (null, boolean, string, finite number)
  • arrays
  • plain objects only (prototype must be Object.prototype or null)
  • Date
  • undefined (distinct from deletion)

Constraints:

  • No root replacement (roots must be containers of the same kind).
  • Arrays are index-based with tail-only adds/deletes (push/pop semantics).
  • Cycles are illegal (diff throws).

Errors

The library throws TreeDiffError with a code:

INVALID_ROOT, CYCLE_DETECTED, UNSUPPORTED_TYPE, TYPE_MISMATCH, PRECONDITION_FAILED, INVALID_META, INVALID_DATE, INVALID_UNDEFINED_ENCODING.

import { TreeDiffError } from "@azuliani/tree-diff";

try {
  apply({ a: 1 }, [["a", "D"]]);
} catch (e) {
  if (e instanceof TreeDiffError) {
    console.error(e.code, e.message);
  }
}

Notes

  • Performance: if a leaf has no meta, apply() may reuse the leaf rhs reference directly. If you need deltas to stay immutable, don’t mutate the patched payload (or deep-clone it first).
  • Full details (normative): see SPEC.md.

Development

npm test
npm run typecheck
npm run build

Benchmarks:

npm run bench