A Speedy E.Meyers's O(ND) Based Diffing Algorithm
This is a re-write of the original Arpad Borsos's diff repository.
As there's no single module in npm that doesn't couple Meyers with strings, and all do more than one thing, I've decided to re-publish it with performance improvements and in a modern ECMAScript fashion. Arpad Borsos agreed so ... here it is.
;// const {NOOP, REPLACE, INSERT, DELETE, diff} = require('speedy-myers'); const changes = ;
The resulting Array
of changes will contain all the information to patch the sourceList
through operations performed via the targetList
.
Example
const NOOP REPLACE INSERT DELETE diff = Myers;const source = 1 2 3 4 5;const target = 2 7 4 5 9 6;const changes = ;let sourceIndex = 0;let targetIndex = 0;for let i = 0 length = changes; i < length; i++ // verify everything went fineconsole;
A DOM Based Example
As this module is general purpose, it is possible to use it to update a tree in the DOM too.
uhtml, lighterhtml, and hyperHTML use a very similar strategy through udomdiff and domdiff.
const NOOP REPLACE INSERT DELETE diff = Myers;const parentNode = documentbody;const source = slice;const target = source;const changes = ;let sourceIndex = 0;let targetIndex = 0;const comments = ;for let i = 0 length = changes; i < length; i++ // once the loop is completed, we can replace all placeholderscomments;
Compatibility
This module is compatible with IE11 and every other Mobile or Desktop engine that supports Int8Array
.