createPatch(fileName, oldStr, newStr, oldHeader?, newHeader?, options?):
same as createTwoFilesPatch(fileName, fileName, ...).
setMemoryLimit(bytes): how much wasm memory stays allocated between calls
(default 32 MB). Wasm memory cannot shrink, so after a call that grows it past
the limit, a new wasm instance replaces it. Infinity keeps all memory.
diffLines and the patch functions accept these jsdiff options:
ignoreWhitespace (default false): compare lines without their leading and
trailing whitespace. A common line keeps the new text.
stripTrailingCr (default false): remove the \r of each \r\n before the
diff.
The patch functions also accept:
context (default 4): unchanged lines around each change. A non-negative
integer, or Infinity for the whole file.
headerOptions (default: all lines on): includeIndex, includeUnderline,
and includeFileHeaders. As in jsdiff, a field left out is off.
Why it is fast
All the work happens in wasm linear memory. JavaScript only writes the input and
reads the result.
Interned tokens: each different token gets a small integer id, so the
Myers O(ND) loop compares ids, not text.
SIMD compare: the inner loop compares 4 ids in one step.
Zero-copy values: each change value is a slice of the input. Wasm returns
only offsets, so no text is decoded.
Hand-written WAT: no compiler sits between the code and the wasm. Each
instruction in the hot path is there on purpose.
Development
moonx patches:build # preprocess the WAT, compile it with wabt and binaryen, then bundle the gluemoonx patches:test # run the tests (they compile the WAT in memory, no build needed)moonx patches:bench # compare throughput with diff@9.0.0moonx patches:bench -- --chart # also update benchmark-{light,dark}.svgmoonx patches:bench -- --memory # also measure the memory that one call allocates