From e96eab03b81641de7b74f22610bb7cd3a2db8432 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Fri, 21 Jul 2023 00:46:22 +0200 Subject: [PATCH] Optimization. --- src/redakcja/static/js/lib/diff.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/redakcja/static/js/lib/diff.js b/src/redakcja/static/js/lib/diff.js index 4dda2740..2ba23ce2 100644 --- a/src/redakcja/static/js/lib/diff.js +++ b/src/redakcja/static/js/lib/diff.js @@ -18,10 +18,25 @@ $.wiki.diff = function(a, b) { x = Vp[k - 1] + 1; } y = x - k; + while (x < N && y < M && a[x] == b[y]) { x ++; y ++; + + // Avoid comparing long text character by character. + let step = 1; + while (true) { + step = Math.min(step * 2, N - x, M - y); + if (!step) break; + if (a.substr(x, step) == b.substr(y, step)) { + x += step; + y += step; + } else { + break; + } + } } + V[k] = x; if (x == N && y == M) { endD = D; @@ -36,7 +51,7 @@ $.wiki.diff = function(a, b) { // Now go back. result = [] - let snake, px, py; + let px, py; for (D = endD; D; --D) { k = x - y; V = VV[D - 1]; -- 2.20.1