Skip to content

Commit c6f4f0b

Browse files
committed
assert: improve myers diff performance
fix: #57242
1 parent 269c851 commit c6f4f0b

File tree

1 file changed

+5
-13
lines changed

1 file changed

+5
-13
lines changed

lib/internal/assert/myers_diff.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const {
44
ArrayPrototypePush,
5-
ArrayPrototypeSlice,
65
Int32Array,
76
StringPrototypeEndsWith,
87
} = primordials;
@@ -16,7 +15,7 @@ function areLinesEqual(actual, expected, checkCommaDisparity) {
1615
return true;
1716
}
1817
if (checkCommaDisparity) {
19-
return `${actual},` === expected || actual === `${expected},`;
18+
return (actual + ',') === expected || actual === (expected + ',');
2019
}
2120
return false;
2221
}
@@ -26,12 +25,10 @@ function myersDiff(actual, expected, checkCommaDisparity = false) {
2625
const expectedLength = expected.length;
2726
const max = actualLength + expectedLength;
2827
const v = new Int32Array(2 * max + 1);
29-
3028
const trace = [];
3129

3230
for (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`
3532

3633
for (let diagonalIndex = -diffLevel; diagonalIndex <= diffLevel; diagonalIndex += 2) {
3734
const offset = diagonalIndex + max;
@@ -89,22 +86,17 @@ function backtrack(trace, actual, expected, checkCommaDisparity) {
8986

9087
while (x > prevX && y > prevY) {
9188
const 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;
9690
ArrayPrototypePush(result, { __proto__: null, type: 'nop', value });
9791
x--;
9892
y--;
9993
}
10094

10195
if (diffLevel > 0) {
10296
if (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
}

0 commit comments

Comments
 (0)