npm.io
0.0.2-0 • Published 4 months ago

@vscode/diff

Licence
MIT
Version
0.0.2-0
Deps
0
Size
428 kB
Vulns
0
Weekly
0

@vscode/diff

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, {
    ignoreTrimWhitespace: false,
    computeMoves: false,
});

// WASM backend (loads the .wasm module on first call)
const wasm = await createDiffComputer({ useWasm: true });
const result2 = wasm.computeDiff(original, modified);

computeDiff returns a DiffResult containing:

  • edits — an AnnotatedStringEdit describing the text-level changes (offset-based, suitable for applying directly).
  • moves — detected block moves (when computeMoves: true).
  • hitTimeouttrue if computation exceeded maxComputationTimeMs.
Options
Option Default Description
maxComputationTimeMs 0 (no limit) Cap the diff time; falls back to a coarser result on timeout.
ignoreTrimWhitespace false Treat lines as equal when they differ only in leading/trailing whitespace.
computeMoves false Detect moved blocks.
extendToSubwords false Refine inner changes to subword boundaries.

Performance

Across all bundled test fixtures (46 cases, total median time in ms):

Backend Total vs TS vs native Rust
TS (V8) 113.7 1.00× 2.86× slower
WASM 72.5 0.64× 1.82× slower
Native Rust (reference) 39.8 0.35× 1.00×

Run node scripts/bench.mjs to reproduce.

License

MIT — see LICENSE.

Keywords