Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2daee76

Browse filesBrowse files
Giovanni Bucciaduh95
authored andcommitted
assert: improve myers diff performance
fix: #57242 PR-URL: #57279 Fixes: #57242 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: LiviaMedeiros <livia@cirno.name>
1 parent 99abfb6 commit 2daee76
Copy full SHA for 2daee76

File tree

Expand file treeCollapse file tree

2 files changed

+43
-13
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+43
-13
lines changed
Open diff view settings
Collapse file
+38Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const assert = require('assert');
4+
5+
const bench = common.createBenchmark(main, {
6+
n: [10, 50, 200, 500],
7+
size: [10, 100],
8+
datasetName: ['objects'],
9+
});
10+
11+
const baseObject = {
12+
a: 1,
13+
b: {
14+
c: 2,
15+
d: [3, 4, 5],
16+
e: 'fghi',
17+
j: {
18+
k: 6,
19+
},
20+
},
21+
};
22+
23+
function createObjects(size) {
24+
return Array.from({ length: size }, () => baseObject);
25+
}
26+
27+
function main({ n, size }) {
28+
bench.start();
29+
for (let i = 0; i < n; ++i) {
30+
new assert.AssertionError({
31+
actual: {},
32+
expected: createObjects(size),
33+
operator: 'partialDeepStrictEqual',
34+
stackStartFunction: () => {},
35+
});
36+
}
37+
bench.end(n);
38+
}
Collapse file

‎lib/internal/assert/myers_diff.js‎

Copy file name to clipboardExpand all lines: lib/internal/assert/myers_diff.js
+5-13Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.