1.1.0 • Published 3 years ago

@edit-distance/myers-1986 v1.1.0

Weekly downloads
-
License
AGPL-3.0
Repository
github
Last release
3 years ago

:abacus: @edit-distance/myers-1986

Reasonably efficient implementation of Eugene W. Myers's exact longest common subsequence and minimum edit-distance algorithm for JavaScript. See docs.

:building_construction: Caveat emptor! This is work in progress. Code may be working. Documentation may be present. Coherence may be. Maybe.

:warning: Only signed 32-bit integers are supported for MAX, xi, xj, yi, yj. This is also true for the values xj - xi and yj - yi but is not enforced. You should be fine if the elements of each pair have the same sign. This means you cannot diff things with more than 2,147,483,647 parts (array elements, string characters, text words or lines).

// Example 1: compute Edit Distance.

import {makeScan, twoWay} from '@edit-distance/myers-1986';

const scan = makeScan(twoWay);

const ed = (x, y) => {
	const eq = (xi, yi) => x[xi] === y[yi];
	const xi = 0;
	const xj = x.length;
	const yi = 0;
	const yj = y.length;
	const MAX = xj - xi + (yj - yi);
	return scan(MAX, eq, xi, xj, yi, yj);
};

ed('BANANA', 'ATANA'); // 3

// Example 2: compute LCS.

import {diff} from '@edit-distance/myers-1986';

const rectangles = (x, y) => {
	const eq = (xi, yi) => x[xi] === y[yi];
	const xi = 0;
	const xj = x.length;
	const yi = 0;
	const yj = y.length;
	const MAX = xj - xi + (yj - yi);
	return diff(MAX, eq, xi, xj, yi, yj);
};

const lcs = (x, y) => {
	let result = x.slice(0, 0);
	let xp = 0;
	for (const [x0, x1] of rectangles(x, y)) {
		result = result.concat(x.slice(xp, x0));
		xp = x1;
	}

	return result.concat(x.slice(xp, x.length));
};

lcs('BANANA', 'ATANA'); // AANA

License Version Tests Dependencies Dev dependencies GitHub issues Downloads

Code issues Code maintainability Code coverage (cov) Code technical debt Documentation Package size

:bicyclist: Benchmark

inputfast-myers-diff v3.0.1modern.js v0.0.1module.js v0.0.1cjs v0.0.1
N+M=220 N=100 M=120 LCS=100 DEL=0 INS=2077,170 ops/sec99,952 ops/sec97,806 ops/sec100,333 ops/sec
N+M=220 N=120 M=100 LCS=100 DEL=20 INS=068,575 ops/sec90,582 ops/sec89,178 ops/sec90,030 ops/sec
N+M=220 N=110 M=110 LCS=100 DEL=10 INS=1063,494 ops/sec83,439 ops/sec81,201 ops/sec82,661 ops/sec
N+M=224 N=14 M=210 LCS=10 DEL=4 INS=2007,112 ops/sec10,346 ops/sec10,406 ops/sec10,898 ops/sec
N+M=20020 N=10000 M=10020 LCS=10000 DEL=0 INS=203,282 ops/sec4,313 ops/sec4,174 ops/sec4,415 ops/sec
N+M=20020 N=10020 M=10000 LCS=10000 DEL=20 INS=03,317 ops/sec4,040 ops/sec4,035 ops/sec4,947 ops/sec
N+M=20020 N=10010 M=10010 LCS=10000 DEL=10 INS=102,414 ops/sec2,939 ops/sec2,843 ops/sec3,009 ops/sec
N+M=220 N=110 M=110 LCS=10 DEL=100 INS=1001,607 ops/sec2,737 ops/sec2,720 ops/sec2,887 ops/sec
N+M=20200 N=10000 M=10200 LCS=10000 DEL=0 INS=200871 ops/sec1,258 ops/sec1,254 ops/sec1,293 ops/sec
N+M=20200 N=10200 M=10000 LCS=10000 DEL=200 INS=0844 ops/sec1,240 ops/sec1,258 ops/sec1,309 ops/sec
N+M=20200 N=10100 M=10100 LCS=10000 DEL=100 INS=100703 ops/sec978 ops/sec976 ops/sec978 ops/sec
N+M=2020 N=1010 M=1010 LCS=10 DEL=1000 INS=100019.10 ops/sec36.35 ops/sec36.45 ops/sec38.16 ops/sec