From 54e8e83aca2f2ce97b70f35c06567be152769b42 Mon Sep 17 00:00:00 2001 From: Ayoub Mabrouk Date: Tue, 21 Oct 2025 02:07:50 +0100 Subject: [PATCH] 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. --- lib/internal/assert/assertion_error.js | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/lib/internal/assert/assertion_error.js b/lib/internal/assert/assertion_error.js index 5c15b96b12d1ea..43ce0508b272d1 100644 --- a/lib/internal/assert/assertion_error.js +++ b/lib/internal/assert/assertion_error.js @@ -149,21 +149,16 @@ function getStackedDiff(actual, expected) { } function getSimpleDiff(originalActual, actual, originalExpected, expected) { - let stringsLen = actual.length + expected.length; + const isStrA = typeof originalActual === 'string'; + const isStrE = typeof originalExpected === 'string'; // Accounting for the quotes wrapping strings - if (typeof originalActual === 'string') { - stringsLen -= 2; - } - if (typeof originalExpected === 'string') { - stringsLen -= 2; - } + const stringsLen = actual.length + expected.length - (isStrA ? 2 : 0) - (isStrE ? 2 : 0); if (stringsLen <= kMaxShortStringLength && (originalActual !== 0 || originalExpected !== 0)) { return { message: `${actual} !== ${expected}`, header: '' }; } - const isStringComparison = typeof originalActual === 'string' && typeof originalExpected === 'string'; // colored myers diff - if (isStringComparison && colors.hasColors) { + if (isStrA && isStrE && colors.hasColors) { return getColoredMyersDiff(actual, expected); }