Skip to content

Commit 342d57a

Browse files
RaisinTendanielleadams
authored andcommitted
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 <darshan.sen@postman.com> PR-URL: #41015 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Zijian Liu <lxxyxzj@gmail.com>
1 parent b265977 commit 342d57a

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

lib/assert.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ function expectsError(stackStartFn, actual, error, message) {
832832
details += ` (${error.name})`;
833833
}
834834
details += message ? `: ${message}` : '.';
835-
const fnType = stackStartFn.name === 'rejects' ? 'rejection' : 'exception';
835+
const fnType = stackStartFn === assert.rejects ? 'rejection' : 'exception';
836836
innerFail({
837837
actual: undefined,
838838
expected: error,
@@ -879,7 +879,7 @@ function expectsNoError(stackStartFn, actual, error, message) {
879879

880880
if (!error || hasMatchingError(actual, error)) {
881881
const details = message ? `: ${message}` : '.';
882-
const fnType = stackStartFn.name === 'doesNotReject' ?
882+
const fnType = stackStartFn === assert.doesNotReject ?
883883
'rejection' : 'exception';
884884
innerFail({
885885
actual,
@@ -999,7 +999,7 @@ function internalMatch(string, regexp, message, fn) {
999999
'regexp', 'RegExp', regexp
10001000
);
10011001
}
1002-
const match = fn.name === 'match';
1002+
const match = fn === assert.match;
10031003
if (typeof string !== 'string' ||
10041004
RegExpPrototypeTest(regexp, string) !== match) {
10051005
if (message instanceof Error) {

0 commit comments

Comments
 (0)