assert: improve myers diff performance · nodejs/node@2daee76
@@ -2,7 +2,6 @@
2233const {
44 ArrayPrototypePush,
5- ArrayPrototypeSlice,
65 Int32Array,
76 StringPrototypeEndsWith,
87} = primordials;
@@ -16,7 +15,7 @@ function areLinesEqual(actual, expected, checkCommaDisparity) {
1615return true;
1716}
1817if (checkCommaDisparity) {
19-return `${actual},` === expected || actual === `${expected},`;
18+return (actual + ',') === expected || actual === (expected + ',');
2019}
2120return false;
2221}
@@ -26,12 +25,10 @@ function myersDiff(actual, expected, checkCommaDisparity = false) {
2625const expectedLength = expected.length;
2726const max = actualLength + expectedLength;
2827const v = new Int32Array(2 * max + 1);
29-3028const trace = [];
31293230for (let diffLevel = 0; diffLevel <= max; diffLevel++) {
33-const newTrace = ArrayPrototypeSlice(v);
34-ArrayPrototypePush(trace, newTrace);
31+ArrayPrototypePush(trace, new Int32Array(v)); // Clone the current state of `v`
35323633for (let diagonalIndex = -diffLevel; diagonalIndex <= diffLevel; diagonalIndex += 2) {
3734const offset = diagonalIndex + max;
@@ -89,22 +86,17 @@ function backtrack(trace, actual, expected, checkCommaDisparity) {
89869087while (x > prevX && y > prevY) {
9188const actualItem = actual[x - 1];
92-const value =
93-!checkCommaDisparity || StringPrototypeEndsWith(actualItem, ',') ?
94-actualItem :
95-expected[y - 1];
89+const value = checkCommaDisparity && !StringPrototypeEndsWith(actualItem, ',') ? expected[y - 1] : actualItem;
9690ArrayPrototypePush(result, { __proto__: null, type: 'nop', value });
9791x--;
9892y--;
9993}
1009410195if (diffLevel > 0) {
10296if (x > prevX) {
103-ArrayPrototypePush(result, { __proto__: null, type: 'insert', value: actual[x - 1] });
104-x--;
97+ArrayPrototypePush(result, { __proto__: null, type: 'insert', value: actual[--x] });
10598} else {
106-ArrayPrototypePush(result, { __proto__: null, type: 'delete', value: expected[y - 1] });
107-y--;
99+ArrayPrototypePush(result, { __proto__: null, type: 'delete', value: expected[--y] });
108100}
109101}
110102}