Skip to content

Object Diff

Typed deep comparison, change records, and JSON Patch generation for structured snapshots.

Install

bash
npm install @jayoncode/object-diff
bash
pnpm add @jayoncode/object-diff
bash
yarn add @jayoncode/object-diff

Example: detect changes and apply a patch

ts
import { diff, hasChanges, patch, applyPatch } from "@jayoncode/object-diff";

const before = { user: { name: "John", role: "viewer" }, active: true };
const after = { user: { name: "Jane", role: "admin" }, active: true };

if (hasChanges(before, after)) {
  const result = diff(before, after);
  const operations = patch(result);
  const synced = applyPatch(before, operations);
  // synced matches `after` for tracked paths
}

Use diff() for audit trails and inspectors; hasChanges() for dirty checks; patch() / applyPatch() to propagate updates between stores or clients.

Inspect changes interactively →

Problem → approach

Typical painObject Diff
JSON.stringify(a) !== JSON.stringify(b) — no paths, no types, order-sensitivediff() returns typed change records with paths (user.name, items[2])
Hand-rolled deep equality and patch logic for optimistic UIpatch() / applyPatch() emit and apply RFC 6902 operations
Explaining what changed in PRs or logs requires custom formattingserialize() exports JSON, Markdown, or table views from the same diff

Overview

Object Diff compares plain objects and arrays, emits structured change records, and can serialize results or produce RFC 6902-style patch operations.

APIPurpose
diff(a, b)Full change list with paths and types
hasChanges(a, b)Boolean dirty check without full diff cost
patch(diffResult)Generate patch operations
applyPatch(target, ops)Immutable apply
serialize(diff, format)JSON, Markdown, or table export

Documentation path

Foundation

#GuideTopicsPlayground
1TutorialInstall, first diffDashboard
2Core conceptsSnapshots, changes, patchesDiff

Core APIs

#GuideTopicsPlayground
3DiffingOptions, filtering, helpersDiff
4PatchingApply, revert, edge casesPatch

Output and performance

#GuideTopicsPlayground
5SerializationExport formatsJSON
6PerformanceLarge-object benchmarksBenchmarks

Package fit

RequirementAPI
Form/state dirty detectionhasChanges(a, b)
Structured audit logdiff() change records
Partial sync between clientspatch() + applyPatch()
Human-readable changelogsserialize(..., "markdown")

Reference

JOC is a JayOnCode monorepo of @jayoncode/* packages. Docs sync from source via TypeDoc and sync scripts.