Skip to content

Commit

Permalink
assert: improve myers diff performance
Browse files Browse the repository at this point in the history
fix: #57242
PR-URL: #57279
Fixes: #57242
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: LiviaMedeiros <[email protected]>
  • Loading branch information
puskin94 authored Mar 6, 2025
1 parent 2a6f908 commit 395439b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
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

0 comments on commit 395439b

Please sign in to comment.