+1
Daily
9,223
stars
557
forks
86
watchers
415
prs
331
issues
0
releases
README
jsdiff
A JavaScript text differencing implementation. Try it out in the online demo.
Based on the algorithm proposed in
"An O(ND) Difference Algorithm and its Variations" (Myers, 1986).
Installation
bash
npm install diff --save
Getting started
Imports
In an environment where you can use imports, everything you need can be imported directly from diff. e.g.
ESM:
import {diffChars, createPatch} from 'diff';
CommonJS
const {diffChars, createPatch} = require('diff');
If you want to serve jsdiff to a web page without using a module system, you can use dist/diff.js or dist/diff.min.js. These create a global called Diff that contains the entire JsDiff API as its properties.
Usage
jsdiff's diff functions all take an old text and a new text and perform three steps:
1. Split both texts into arrays of "tokens". What constitutes a token varies; in diffChars, each character is a token, while in diffLines, each line is a token.
2. Find the smallest set of single-token insertions and deletions needed to transform the first array of tokens into the second.
This step depends upon having some notion of a token from the old array being "equal" to one from the new array, and this notion of equality affects the results. Usually two tokens are equal if === considers them equal, but some of the diff functions use an alternative notion of equality or have options to configure it. For instance, by default diffChars("Foo", "FOOD") will require two deletions (o, o) and three insertions (O, O, D), but diffChars("Foo", "FOOD", {ignoreCase: true}) will require just one insertion (of a D), since ignoreCase causes o and O to be considered equal.
3. Return an array representing the transformation computed in the previous step as a series of change objects. The array is ordered from the start of the input to the end, and each change object represents inserting one or more tokens, deleting one or more tokens, or keeping one or more tokens.
API
diffChars(oldStr, newStr[, options]) - diffs two blocks of text, treating each character as a token.
("Characters" here means Unicode code points - the elements you get when you loop over a string with a for ... of ... loop.)
Returns a list of change objects.
Options
ignoreCase: If true, the uppercase and lowercase forms of a character are considered equal. Defaults to false.
diffWords(oldStr, newStr[, options]) - diffs two blocks of text, treating each word and each punctuation mark as a token. Whitespace is ignored when computing the diff (but preserved as far as possible in the final change objects).
Returns a list of change objects.
Options
ignoreCase: Same as in diffChars. Defaults to false.
intlSegmenter: An optional Intl.Segmenter object (which must have a granularity of 'word') for diffWords to use to split the text into words.
By default, diffWords does not use an Intl.Segmenter, just some regexes for splitting text into words. This will tend to give woTop issues
#1813-way merge14 comments · 2017-06-05#396Suboptimal (O(D^2)) memory usage10 comments · 2023-03-03#629reversing a text diff?7 comments · 2025-09-22#624Improve integration with `react-diff-view` and investigate if `unidiff` can be entirely obsoleted and deprecated. (Originally: "Is there a way to remove the `======` separator that appears at the top of diffs?")6 comments · 2025-07-24#79Near Endless Loop calling execEditLength4 comments · 2015-10-07#221Feature Request: ignorePunctuation2 comments · 2018-05-29#176Feature Request: Apply to a change to a String2 comments · 2017-04-23#167How to minimize the number of insertions?2 comments · 2017-02-15