JavaScript Text Differencing Library

9.0.0 · active · verified Sun Apr 19

The `diff` (also known as `jsdiff`) library provides a robust JavaScript implementation for calculating differences between texts. Based on the efficient Myers O(ND) algorithm, it offers various comparison granularities, including character, word, line, CSS token, and JSON object differencing. The library is currently at stable version 9.0.0, released recently (April 2026), and maintains an active development cycle with frequent updates addressing performance, features, and critical bug fixes. Major versions, particularly v6 and v8, have introduced significant breaking changes and refactors, including a transition to bundled TypeScript types in v8. `jsdiff` stands out for its comprehensive API, enabling not only difference calculation but also the creation and application of unified diff patches. It supports modern Node.js environments and widely available browser runtimes, and is a zero-dependency package, making it a lightweight yet powerful utility for tasks like code reviews, version control, and data synchronization.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates a basic line-by-line text comparison and logs the changes with indicators.

import { diffLines } from 'diff';

const oldText = `Line One
Line Two
Line Three
Line Four`;
const newText = `Line A
Line Two Changed
Line Three
Line B`;

const changes = diffLines(oldText, newText);

console.log('--- Diff Results ---');
changes.forEach((part) => {
  const color = part.added ? 'green' : part.removed ? 'red' : 'grey';
  // For a real-world scenario, use a color library or styling for output
  const prefix = part.added ? '+ ' : part.removed ? '- ' : '  ';
  console.log(`${prefix}${part.value.replace(/\n/g, '\n' + prefix)}`, `(${color})`);
});

/* Example Output:
--- Diff Results ---
+ Line A (green)
- Line One (red)
  Line Two (grey)
+  Changed (green)
- Line Two (red)
  Line Three (grey)
+ Line B (green)
- Line Four (red)
*/

view raw JSON →