Fast Diff
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
-
TypeError: diff is not a function
cause Attempting to call `diff` as a named export (`import { diff } from 'fast-diff'`) or incorrectly accessing a default export from a CommonJS module in an ESM context.fixUse a default import: `import diff from 'fast-diff';`. If you are in a CommonJS module, use `const diff = require('fast-diff');`. -
Property 'match' does not exist on type '(...args: any[]) => DiffResult[]'.
cause Attempting to access `match` or `patch` functions, which were explicitly removed from this simplified `diff-match-patch` port.fixThe `fast-diff` library only provides the diffing functionality. If you require `match` or `patch` operations, you need to use the original `diff-match-patch` library or an alternative that includes those features.
Warnings
- gotcha This library is a 'simplified import' of `diff-match-patch`. It explicitly removes the 'match' and 'patch' functionalities, providing only the core diffing algorithm. If you need matching or patching capabilities, you will need to use the original `diff-match-patch` library or another alternative.
- gotcha The package's last release (v1.3.0) was in October 2020. While stable, this indicates a maintenance-only status rather than active feature development. Users seeking frequent updates, new features, or rapid bug fixes might find the project's pace slow.
- gotcha Older Node.js or bundler configurations might struggle with `fast-diff`'s CommonJS-first export style when used with modern ESM `import` statements, leading to issues like 'diff is not a function' or unexpected `default` property access.
Install
-
npm install fast-diff -
yarn add fast-diff -
pnpm add fast-diff
Imports
- diff
const diff = require('fast-diff');import diff from 'fast-diff';
- INSERT, EQUAL, DELETE
const { INSERT, EQUAL, DELETE } = require('fast-diff');import { INSERT, EQUAL, DELETE } from 'fast-diff';
Quickstart
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