Skip to content

Commit 54e8e83

Browse files
committed
assert: optimize string length calculation in getSimpleDiff
Reduced redundant typeof operations by caching type checks and simplified the string length calculation using ternary operators for improved readability.
1 parent 82fc81c commit 54e8e83

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

lib/internal/assert/assertion_error.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,21 +149,16 @@ function getStackedDiff(actual, expected) {
149149
}
150150

151151
function getSimpleDiff(originalActual, actual, originalExpected, expected) {
152-
let stringsLen = actual.length + expected.length;
152+
const isStrA = typeof originalActual === 'string';
153+
const isStrE = typeof originalExpected === 'string';
153154
// Accounting for the quotes wrapping strings
154-
if (typeof originalActual === 'string') {
155-
stringsLen -= 2;
156-
}
157-
if (typeof originalExpected === 'string') {
158-
stringsLen -= 2;
159-
}
155+
const stringsLen = actual.length + expected.length - (isStrA ? 2 : 0) - (isStrE ? 2 : 0);
160156
if (stringsLen <= kMaxShortStringLength && (originalActual !== 0 || originalExpected !== 0)) {
161157
return { message: `${actual} !== ${expected}`, header: '' };
162158
}
163159

164-
const isStringComparison = typeof originalActual === 'string' && typeof originalExpected === 'string';
165160
// colored myers diff
166-
if (isStringComparison && colors.hasColors) {
161+
if (isStrA && isStrE && colors.hasColors) {
167162
return getColoredMyersDiff(actual, expected);
168163
}
169164

0 commit comments

Comments
 (0)