From b5e1342a50c1ca6c9c76c195219317971843d684 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Mon, 29 Nov 2021 18:13:28 +0530 Subject: [PATCH] assert: prefer reference comparison over string comparison Pointer comparison takes constant time and string comparison takes linear time unless there is some string interning going on, so this might be a little bit faster. Signed-off-by: Darshan Sen --- lib/assert.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/assert.js b/lib/assert.js index 89949c01614fd4..e0ef3ad63ea6b1 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -832,7 +832,7 @@ function expectsError(stackStartFn, actual, error, message) { details += ` (${error.name})`; } details += message ? `: ${message}` : '.'; - const fnType = stackStartFn.name === 'rejects' ? 'rejection' : 'exception'; + const fnType = stackStartFn === assert.rejects ? 'rejection' : 'exception'; innerFail({ actual: undefined, expected: error, @@ -879,7 +879,7 @@ function expectsNoError(stackStartFn, actual, error, message) { if (!error || hasMatchingError(actual, error)) { const details = message ? `: ${message}` : '.'; - const fnType = stackStartFn.name === 'doesNotReject' ? + const fnType = stackStartFn === assert.doesNotReject ? 'rejection' : 'exception'; innerFail({ actual, @@ -993,7 +993,7 @@ function internalMatch(string, regexp, message, fn) { 'regexp', 'RegExp', regexp ); } - const match = fn.name === 'match'; + const match = fn === assert.match; if (typeof string !== 'string' || RegExpPrototypeTest(regexp, string) !== match) { if (message instanceof Error) {