From 99fb6d48eb19ecfe0f61860049665a7305d13f51 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Tue, 14 Dec 2021 20:28:47 +0530 Subject: [PATCH] assert: prefer reference comparison over string comparison MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 PR-URL: https://github.com/nodejs/node/pull/41015 Reviewed-By: Ruben Bridgewater Reviewed-By: Tobias Nießen Reviewed-By: Luigi Pinca Reviewed-By: Benjamin Gruenbaum Reviewed-By: James M Snell Reviewed-By: Zijian Liu --- lib/assert.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/assert.js b/lib/assert.js index 1998246e09c15a..9a684cbe5fd6bf 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -831,7 +831,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, @@ -878,7 +878,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, @@ -998,7 +998,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) {