Skip to content

assert: improve myers diff performance #57279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions benchmark/assert/assertion-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';
const common = require('../common.js');
const assert = require('assert');

const bench = common.createBenchmark(main, {
n: [10, 50, 200, 500],
size: [10, 100],
datasetName: ['objects'],
});

const baseObject = {
a: 1,
b: {
c: 2,
d: [3, 4, 5],
e: 'fghi',
j: {
k: 6,
},
},
};

function createObjects(size) {
return Array.from({ length: size }, () => baseObject);
}

function main({ n, size }) {
bench.start();
for (let i = 0; i < n; ++i) {
new assert.AssertionError({
actual: {},
expected: createObjects(size),
operator: 'partialDeepStrictEqual',
stackStartFunction: () => {},
});
}
bench.end(n);
}
18 changes: 5 additions & 13 deletions lib/internal/assert/myers_diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const {
ArrayPrototypePush,
ArrayPrototypeSlice,
Int32Array,
StringPrototypeEndsWith,
} = primordials;
Expand All @@ -16,7 +15,7 @@ function areLinesEqual(actual, expected, checkCommaDisparity) {
return true;
}
if (checkCommaDisparity) {
return `${actual},` === expected || actual === `${expected},`;
return (actual + ',') === expected || actual === (expected + ',');
}
return false;
}
Expand All @@ -26,12 +25,10 @@ function myersDiff(actual, expected, checkCommaDisparity = false) {
const expectedLength = expected.length;
const max = actualLength + expectedLength;
const v = new Int32Array(2 * max + 1);

const trace = [];

for (let diffLevel = 0; diffLevel <= max; diffLevel++) {
const newTrace = ArrayPrototypeSlice(v);
ArrayPrototypePush(trace, newTrace);
ArrayPrototypePush(trace, new Int32Array(v)); // Clone the current state of `v`

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

while (x > prevX && y > prevY) {
const actualItem = actual[x - 1];
const value =
!checkCommaDisparity || StringPrototypeEndsWith(actualItem, ',') ?
actualItem :
expected[y - 1];
const value = checkCommaDisparity && !StringPrototypeEndsWith(actualItem, ',') ? expected[y - 1] : actualItem;
ArrayPrototypePush(result, { __proto__: null, type: 'nop', value });
x--;
y--;
}

if (diffLevel > 0) {
if (x > prevX) {
ArrayPrototypePush(result, { __proto__: null, type: 'insert', value: actual[x - 1] });
x--;
ArrayPrototypePush(result, { __proto__: null, type: 'insert', value: actual[--x] });
} else {
ArrayPrototypePush(result, { __proto__: null, type: 'delete', value: expected[y - 1] });
y--;
ArrayPrototypePush(result, { __proto__: null, type: 'delete', value: expected[--y] });
}
}
}
Expand Down
Loading