Fast Diff

1.3.0 · maintenance · verified Sun Apr 19

fast-diff is a JavaScript utility for computing fast text differences between two strings. It is a simplified port of Google's `diff-match-patch` library, specifically optimized for diffing and excluding the match and patch functionalities. The library implements "An O(ND) Difference Algorithm and its Variations" (Myers, 1986) with additional optimizations. The current stable version is 1.3.0, released in October 2020. Its primary differentiator is its focus on high-performance string diffing without the overhead of pattern matching or patching, making it suitable for scenarios where only the differences are needed. The project has an infrequent release cadence, primarily focusing on stability rather than active feature development, as it's a mature port of a well-established algorithm.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic string diffing, including the optional cursor position parameter, and usage of the exported constants.

import diff from 'fast-diff';

const good = 'Good dog';
const bad = 'Bad dog';

const result = diff(good, bad);
// result: [[-1, "Goo"], [1, "Ba"], [0, "d dog"]]

// Respect suggested edit location (cursor position), added in v1.1
const resultWithCursor = diff('aaa', 'aaaa', 1);
// resultWithCursor: [[0, "a"], [1, "a"], [0, "aa"]]

import { INSERT, EQUAL, DELETE } from 'fast-diff';

console.log(`Insert code: ${INSERT}`); // Expected: 1
console.log(`Equal code: ${EQUAL}`);   // Expected: 0
console.log(`Delete code: ${DELETE}`); // Expected: -1

view raw JSON →