diff

A battle-tested JavaScript library for computing and applying text diffs at the character, word, line, sentence, CSS, or JSON level.

Library
npm
v9.0.0
9,213 stars
BSD 3-Clause License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
71 /100 Good
Development Activity 76
Maintenance 36
Community 72
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
84 /100 Excellent
Architecture 85
Code Quality 92
Innovation 80
Learning Curve 78

diff (published to npm as “diff”, developed under the jsdiff project name) is a JavaScript text-differencing library based on Eugene Myers’ O(ND) difference algorithm. It exposes a family of functions — diffChars, diffWords, diffLines, diffSentences, diffCss, diffJson, and diffArrays — that each compute the minimal set of insertions and deletions needed to transform one input into another, at a different granularity of “token”.

Beyond raw diffing, the library includes a full unified-diff patch toolkit: createPatch/createTwoFilesPatch to generate Git-style patch text, parsePatch to read it back into structured hunks, applyPatch/applyPatches to apply hunks to a string, and reversePatch to invert a patch. It ships as dual ESM/CJS with first-class TypeScript types and has been a dependency of the JavaScript ecosystem for over a decade, powering diff views in tools ranging from CI output formatters to code-review UIs.

What You Get

  • Granular diff functions — diffChars, diffWords, diffWordsWithSpace, diffLines, diffTrimmedLines, diffSentences, diffCss, diffJson, and diffArrays — each returning an ordered array of change objects marking additions, removals, and unchanged spans
  • A full patch toolkit: createPatch/createTwoFilesPatch/formatPatch to generate unified-diff text, parsePatch to parse it, applyPatch/applyPatches to apply hunks, and reversePatch to invert them
  • Fine-grained diff options per function — ignoreCase, ignoreWhitespace, newlineIsToken, custom comparators, Intl.Segmenter-based word tokenization, and stringifyReplacer for JSON diffing
  • Abortable/async diffing via a timeout or maxEditLength option and an optional callback, so large diffs don’t block the main thread indefinitely
  • Interop helpers — convertChangesToDMP and convertChangesToXML — for bridging jsdiff’s change-object format to Google’s diff-match-patch format or to XML markup
  • Dual ESM/CommonJS builds with bundled TypeScript declarations and a browser-ready UMD bundle (dist/diff.js) exposing a global Diff object

Common Use Cases

  • Rendering side-by-side or inline code/text diff views in editors, code-review tools, and documentation-comparison UIs
  • Generating and applying Git-style unified-diff patches programmatically, e.g. for scaffolding tools that need to patch generated files
  • Comparing structured JSON payloads (config files, API responses, test fixtures) with human-readable diff output
  • Building CLI or CI tooling that needs to show “before vs after” text changes, such as snapshot-testing diff output or changelog generation

Under The Hood

Architecture The library is organized around a single generic Diff base class (src/diff/base.ts) that implements Myers’ O(ND) algorithm over an edit graph, tracked as a linked list of DraftChangeObject nodes per diagonal path (the bestPath array) to avoid the naive algorithm’s quadratic overextension past the graph’s edges — a documented deviation from the original paper for performance. Each concrete diff mode (src/diff/character.ts, word.ts, line.ts, sentence.ts, css.ts, json.ts, array.ts) subclasses Diff and overrides tokenize, equals, castInput, and join to define what a “token” means for that mode — a Unicode code point for diffChars, a line for diffLines, a canonicalized JSON line for diffJson. The patch toolkit (src/patch/create.ts, parse.ts, apply.ts, reverse.ts) is a separate, loosely coupled layer built on top of diffLines that serializes/deserializes the standard unified-diff hunk format, including Git-specific extended headers and C-style filename quoting for non-ASCII paths. Tech Stack Written entirely in TypeScript, targeting both CommonJS and ESM output via a dual tsc build (libcjs/, libesm/) plus a Rollup+UglifyJS browser bundle (dist/diff.js). Zero runtime dependencies. Uses Mocha/Chai/nyc for testing, ESLint with typescript-eslint for linting, and @arethetypeswrong/cli/tsd to validate the published type declarations and package exports map against real consumers. Code Quality The nyc configuration enforces 100% branch, line, function, and statement coverage (check-coverage: true with all four thresholds at 100 in package.json), and the test suite (~6,900 lines) is roughly double the size of the library’s own source (~3,700 lines), covering each diff mode and every patch operation exhaustively, including edge cases like CRLF handling, empty-file patches, and non-ASCII filename quoting. Source files are heavily commented, including a rationale comment in base.ts explicitly explaining a deliberate deviation from Myers’ published algorithm for a major performance win. API Design The public API favors small, purpose-named functions (diffChars, diffLines, createPatch, applyPatch) over a single configurable entry point, so call sites read close to plain English. Every diff function accepts a consistent options object shape (comparator, ignoreCase/ignoreWhitespace variants, timeout, callback) reused across modes, and TypeScript overloads precisely encode the differing return types for sync vs. callback vs. abortable-async calls, so consumers get accurate autocomplete and type-checking without reading the docs.

Used by 70 apps in this directory

TypeScript
44%
Other

Magic

AI Agents · Automation · Low Code Platforms

5,055

Magic is an enterprise-grade open-source AI agent platform combining a generalist AI agent, workflow engine, IM, and collaborative office system for running an AI-powered digital workforce.

View details
68
Repo Health
79
Technical
65
Dependency
Built with
TypeScript 44%
PHP 32%
Updated 2 months ago
Rust
60%
Apache 2.0

Magnitude

AI Development · Developer Tools

7,313

An open source inference engine that profiles your hardware, recommends the best local models, and tunes them for the coding agent you already use.

View details
79
Repo Health
83
Technical
71
Dependency
Built with
Rust 60%
TypeScript 25%
Updated 6 days ago
TypeScript
99%
Apache 2.0

Mastra Code

AI Code Assistants

29,041

"A coding agent that never compacts" — a terminal-based AI coding agent built on the Mastra framework, with Observational Memory instead of context compaction, multi-model support, and OAuth login for Claude Max or ChatGPT Plus.

View details
90
Repo Health
73
Technical
65
Dependency
Built with
TypeScript 99%
Updated 6 days ago
TypeScript
95%
MIT

melty

AI Code Assistants · Code Editors · Developer Tools

5,462

The AI code editor where every chat message is a git commit you can revert, branch, or squash

View details
35
Repo Health
75
Technical
67
Dependency
Built with
TypeScript 95%
Updated 1 years ago
Clojure
57%
Other

Metabase

Analytics

49,827

The open-source BI platform that lets anyone ask questions and build dashboards without writing SQL — with an embedded analytics SDK and AI-powered query assistant included.

View details
95
Repo Health
84
Technical
62
Dependency
Built with
Clojure 57%
TypeScript 38%
Updated 6 days ago
Python
58%
Apache 2.0

MLflow

AI Development · Monitoring

28,512

The open source AI engineering platform for debugging, evaluating, monitoring, and optimizing production LLMs and agents at scale.

View details
96
Repo Health
86
Technical
67
Dependency
Built with
Python 58%
TypeScript 35%
Updated 6 days ago
TypeScript
99%
Other

NocoBase

Low Code Platforms · No Code Platforms

24,686

Open-source AI + no-code platform that lets coding agents and people collaborate to build business systems fast on proven infrastructure.

View details
94
Repo Health
81
Technical
63
Dependency
Built with
TypeScript 99%
Updated 6 days ago
TypeScript
65%
Other

NocoDB

Databases · Low Code Platforms · No Code Platforms

65,340

Turn any SQL database into a collaborative no-code spreadsheet with automatic REST APIs and real-time views.

View details
91
Repo Health
77
Technical
63
Dependency
Built with
TypeScript 65%
Vue 30%
Updated 6 days ago
JavaScript
88%
GPL 3.0

NodeBB

Community

15,242

Modern Node.js forum software with real-time WebSockets, multi-database support, and a plugin ecosystem — the community platform built for the open web and the Fediverse.

View details
97
Repo Health
78
Technical
70
Dependency
Built with
JavaScript 88%
Go Template 11%
Updated 6 days ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers