Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
RaisinTen authored and danielleadams committed Jan 31, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent def6536 commit bfb4fc0
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
@@ -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) {

0 comments on commit bfb4fc0

Please sign in to comment.