⚠️ Experimental — this package is in early development. The API may change without notice and bugs are expected. Do not depend on it for production workloads yet.
A high-performance diff library, ported from the diff algorithm used inside VS Code. Ships with two interchangeable backends:
TypeScript (default) — pure JS, no native dependencies.
WebAssembly — same algorithm compiled from Rust via wasm-pack. Typically ~1.5–3× faster on larger inputs.
Both produce identical results.
Install
npm install @vscode/diff
Usage
import { createDiffComputer } from '@vscode/diff';// TS backend (synchronous-feeling, no init required)const ts = await createDiffComputer();const result = ts.computeDiff(original, modified, { computeMoves: false,});// WASM backend (loads the .wasm module on first call)const wasm = await createDiffComputer({ useWasm: true });const result2 = wasm.computeDiff(original, modified);
TypeScript-only entry point
@vscode/diff/core contains the TypeScript backend and the result types, with no reference to the WASM module or the worker. Bundle it statically (for example into an editor that cannot load .wasm files) and create the computer synchronously:
import { createTsDiffComputer } from '@vscode/diff/core';const result = createTsDiffComputer().computeDiff(original, modified);
Both entry points share the same classes, so results from either can be mixed.
edits — an AnnotatedStringEdit describing the text-level changes (offset-based, suitable for applying directly).
moves — detected moves of whole lines (when computeMoves: true; TypeScript backend only). The moved text is also part of edits; a move tells which deletion and insertion belong together, and its innerEdit describes the changes within the moved text.
hitTimeout — true if computation exceeded maxComputationTimeMs.
Results are always correct: edits.apply(original) === modified. Whitespace changes are never dropped. Replacements that only change leading or trailing whitespace of a line (e.g. indentation) are annotated with DiffAnnotation.trimWhitespace (r.data.isTrimWhitespaceChange), so a UI can hide them instead of asking the algorithm to ignore them. Whitespace changes inside a line are regular changes.
Options
Option
Default
Description
maxComputationTimeMs
0 (no limit)
Cap the diff time; falls back to a coarser result on timeout.
computeMoves
false
Detect moved blocks of lines (TypeScript backend only).
extendToSubwords
false
Refine inner changes to subword boundaries.
Performance
Across all bundled test fixtures (46 cases, total median time in ms):