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 <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>
  • Loading branch information
RaisinTen authored and danielleadams committed Jan 31, 2022
1 parent def6536 commit bfb4fc0
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/assert.js
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 bfb4fc0

Please sign in to comment.