Skip to content

Commit

Permalink
assert: prefer reference comparison over string comparison
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
RaisinTen authored and danielleadams committed Dec 17, 2021
1 parent d8a2125 commit 99fb6d4
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 99fb6d4

Please sign in to comment.