Skip to content

Commit 525ec2c

Browse files
committed
assert: make myers_diff function more performant
1 parent 0d00511 commit 525ec2c

File tree

1 file changed

+32
-27
lines changed

1 file changed

+32
-27
lines changed

lib/internal/assert/myers_diff.js

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
'use strict';
22

33
const {
4-
Array,
5-
ArrayPrototypeFill,
64
ArrayPrototypePush,
75
ArrayPrototypeSlice,
6+
Int32Array,
87
StringPrototypeEndsWith,
98
} = primordials;
109

@@ -26,7 +25,7 @@ function myersDiff(actual, expected, checkCommaDisparity = false) {
2625
const actualLength = actual.length;
2726
const expectedLength = expected.length;
2827
const max = actualLength + expectedLength;
29-
const v = ArrayPrototypeFill(Array(2 * max + 1), 0);
28+
const v = new Int32Array(2 * max + 1);
3029

3130
const trace = [];
3231

@@ -35,22 +34,29 @@ function myersDiff(actual, expected, checkCommaDisparity = false) {
3534
ArrayPrototypePush(trace, newTrace);
3635

3736
for (let diagonalIndex = -diffLevel; diagonalIndex <= diffLevel; diagonalIndex += 2) {
37+
const offset = diagonalIndex + max;
3838
let x;
39-
if (diagonalIndex === -diffLevel ||
40-
(diagonalIndex !== diffLevel && v[diagonalIndex - 1 + max] < v[diagonalIndex + 1 + max])) {
41-
x = v[diagonalIndex + 1 + max];
39+
if (
40+
diagonalIndex === -diffLevel ||
41+
(diagonalIndex !== diffLevel && v[offset - 1] < v[offset + 1])
42+
) {
43+
x = v[offset + 1];
4244
} else {
43-
x = v[diagonalIndex - 1 + max] + 1;
45+
x = v[offset - 1] + 1;
4446
}
4547

4648
let y = x - diagonalIndex;
4749

48-
while (x < actualLength && y < expectedLength && areLinesEqual(actual[x], expected[y], checkCommaDisparity)) {
50+
while (
51+
x < actualLength &&
52+
y < expectedLength &&
53+
areLinesEqual(actual[x], expected[y], checkCommaDisparity)
54+
) {
4955
x++;
5056
y++;
5157
}
5258

53-
v[diagonalIndex + max] = x;
59+
v[offset] = x;
5460

5561
if (x >= actualLength && y >= expectedLength) {
5662
return backtrack(trace, actual, expected, checkCommaDisparity);
@@ -71,10 +77,13 @@ function backtrack(trace, actual, expected, checkCommaDisparity) {
7177
for (let diffLevel = trace.length - 1; diffLevel >= 0; diffLevel--) {
7278
const v = trace[diffLevel];
7379
const diagonalIndex = x - y;
74-
let prevDiagonalIndex;
80+
const offset = diagonalIndex + max;
7581

76-
if (diagonalIndex === -diffLevel ||
77-
(diagonalIndex !== diffLevel && v[diagonalIndex - 1 + max] < v[diagonalIndex + 1 + max])) {
82+
let prevDiagonalIndex;
83+
if (
84+
diagonalIndex === -diffLevel ||
85+
(diagonalIndex !== diffLevel && v[offset - 1] < v[offset + 1])
86+
) {
7887
prevDiagonalIndex = diagonalIndex + 1;
7988
} else {
8089
prevDiagonalIndex = diagonalIndex - 1;
@@ -84,8 +93,10 @@ function backtrack(trace, actual, expected, checkCommaDisparity) {
8493
const prevY = prevX - prevDiagonalIndex;
8594

8695
while (x > prevX && y > prevY) {
87-
const value = !checkCommaDisparity ||
88-
StringPrototypeEndsWith(actual[x - 1], ',') ? actual[x - 1] : expected[y - 1];
96+
const value =
97+
!checkCommaDisparity || StringPrototypeEndsWith(actual[x - 1], ',') ?
98+
actual[x - 1] :
99+
expected[y - 1];
89100
ArrayPrototypePush(result, { __proto__: null, type: 'nop', value });
90101
x--;
91102
y--;
@@ -107,16 +118,11 @@ function backtrack(trace, actual, expected, checkCommaDisparity) {
107118

108119
function printSimpleMyersDiff(diff) {
109120
let message = '';
110-
121+
111122
for (let diffIdx = diff.length - 1; diffIdx >= 0; diffIdx--) {
112123
const { type, value } = diff[diffIdx];
113-
if (type === 'insert') {
114-
message += `${colors.green}${value}${colors.white}`;
115-
} else if (type === 'delete') {
116-
message += `${colors.red}${value}${colors.white}`;
117-
} else {
118-
message += `${colors.white}${value}${colors.white}`;
119-
}
124+
const color = type === 'insert' ? colors.green : type === 'delete' ? colors.red : colors.white;
125+
message += `${color}${value}${colors.white}`;
120126
}
121127

122128
return `\n${message}`;
@@ -129,17 +135,16 @@ function printMyersDiff(diff, simple = false) {
129135

130136
for (let diffIdx = diff.length - 1; diffIdx >= 0; diffIdx--) {
131137
const { type, value } = diff[diffIdx];
132-
const previousType = (diffIdx < (diff.length - 1)) ? diff[diffIdx + 1].type : null;
133-
const typeChanged = previousType && (type !== previousType);
138+
const previousType = diffIdx < diff.length - 1 ? diff[diffIdx + 1].type : null;
134139

135-
if (typeChanged && previousType === 'nop') {
136-
// Avoid grouping if only one line would have been grouped otherwise
140+
// Avoid grouping if only one line would have been grouped otherwise
141+
if (previousType === 'nop' && type !== previousType) {
137142
if (nopCount === kNopLinesToCollapse + 1) {
138143
message += `${colors.white} ${diff[diffIdx + 1].value}\n`;
139144
} else if (nopCount === kNopLinesToCollapse + 2) {
140145
message += `${colors.white} ${diff[diffIdx + 2].value}\n`;
141146
message += `${colors.white} ${diff[diffIdx + 1].value}\n`;
142-
} if (nopCount >= (kNopLinesToCollapse + 3)) {
147+
} else if (nopCount >= kNopLinesToCollapse + 3) {
143148
message += `${colors.blue}...${colors.white}\n`;
144149
message += `${colors.white} ${diff[diffIdx + 1].value}\n`;
145150
skipped = true;

0 commit comments

Comments
 (0)