spacer

Javascript Diff Algorithm


Using an idea grabbed from a mailing list post, I implemented the diff algorithm discussed in the following paper (free registration required):

P. Heckel, A technique for isolating differences between files
Comm. ACM, 21, (4), 264--268 (1978).

The implementation, itself, has two functions, one of which is recommended for use:

diffString( String oldFile, String newFile )
This method takes two strings and calculates the differences in each. The final result is the 'newFile' marked up with HTML (to signify both deletions from the oldFile and additions to the newFile).

Sample Code

document.body.innerHTML = diffString(
   "The red brown fox jumped over the rolling log.",
   "The brown spotted fox leaped over the rolling log"
);

Sample Output

The red brown spotted fox jumped leaped over the rolling log.

Projects Using this Code:

  • Wikipedia Animate
  • AniWiki

Downloads

  • jsdiff.js

This work is licensed under the MIT License.

Posted: June 23rd, 2005 ·

If you particularly enjoy my work, I appreciate donations given with Gittip:

49 Comments (Show Comments)



Comments are closed.
Comments are automatically turned off two weeks after the original post. If you have a question concerning the content of this post, please feel free to contact me.


JavaScript Books

spacer

JavaScript Secrets

Secret techniques of top JavaScript programmers.

spacer

Pro JavaScript

The best techniques for professional JavaScript. Published by Apress.

Micro Updates

spacer

@jeresig

Infrequent, short, updates and links.

JavaScript Jobs

Ninja Jobs: JavaScript Jobs



via Ad Packs

VPS Hosting Provided by Media Temple

Ukiyo-e: Japanese Woodblock Prints
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.