From: Radek Czajka Date: Thu, 20 Jul 2023 22:46:22 +0000 (+0200) Subject: Optimization. X-Git-Url: https://git.mdrn.pl/redakcja.git/commitdiff_plain/e96eab03b81641de7b74f22610bb7cd3a2db8432 Optimization. --- 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];