Skip to content

Commit 99fb6d4

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 <[email protected]> PR-URL: #41015 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Zijian Liu <[email protected]>
1 parent d8a2125 commit 99fb6d4

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

lib/assert.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ function expectsError(stackStartFn, actual, error, message) {
831831
details += ` (${error.name})`;
832832
}
833833
details += message ? `: ${message}` : '.';
834-
const fnType = stackStartFn.name === 'rejects' ? 'rejection' : 'exception';
834+
const fnType = stackStartFn === assert.rejects ? 'rejection' : 'exception';
835835
innerFail({
836836
actual: undefined,
837837
expected: error,
@@ -878,7 +878,7 @@ function expectsNoError(stackStartFn, actual, error, message) {
878878

879879
if (!error || hasMatchingError(actual, error)) {
880880
const details = message ? `: ${message}` : '.';
881-
const fnType = stackStartFn.name === 'doesNotReject' ?
881+
const fnType = stackStartFn === assert.doesNotReject ?
882882
'rejection' : 'exception';
883883
innerFail({
884884
actual,
@@ -998,7 +998,7 @@ function internalMatch(string, regexp, message, fn) {
998998
'regexp', 'RegExp', regexp
999999
);
10001000
}
1001-
const match = fn.name === 'match';
1001+
const match = fn === assert.match;
10021002
if (typeof string !== 'string' ||
10031003
RegExpPrototypeTest(regexp, string) !== match) {
10041004
if (message instanceof Error) {

0 commit comments

Comments
 (0)