◐ Shell
clean mode source ↗

assert: improve myers diff performance · nodejs/node@2daee76

@@ -2,7 +2,6 @@

2233

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 = [];

31293230

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`

35323633

for (let diagonalIndex = -diffLevel; diagonalIndex <= diffLevel; diagonalIndex += 2) {

3734

const offset = diagonalIndex + max;

@@ -89,22 +86,17 @@ function backtrack(trace, actual, expected, checkCommaDisparity) {

89869087

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

}

1009410195

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

}