Skip to content

Commit 645d056

Browse files
committed
assert: use a set instead of an array for faster lookup
This is a very small improvement for error creation.
1 parent 9498747 commit 645d056

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

lib/internal/assert/assertion_error.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const {
1010
ObjectDefineProperty,
1111
ObjectGetPrototypeOf,
1212
ObjectPrototypeHasOwnProperty,
13+
SafeSet,
1314
String,
1415
StringPrototypeRepeat,
1516
StringPrototypeSlice,
@@ -42,7 +43,10 @@ const kReadableOperator = {
4243
const kMaxShortStringLength = 12;
4344
const kMaxLongStringLength = 512;
4445

45-
const kMethodsWithCustomMessageDiff = ['deepStrictEqual', 'strictEqual', 'partialDeepStrictEqual'];
46+
const kMethodsWithCustomMessageDiff = new SafeSet()
47+
.add('deepStrictEqual')
48+
.add('strictEqual')
49+
.add('partialDeepStrictEqual');
4650

4751
function copyError(source) {
4852
const target = ObjectAssign(
@@ -263,7 +267,7 @@ class AssertionError extends Error {
263267
if (isErrorStackTraceLimitWritable()) Error.stackTraceLimit = 0;
264268

265269
if (message != null) {
266-
if (kMethodsWithCustomMessageDiff.includes(operator)) {
270+
if (kMethodsWithCustomMessageDiff.has(operator)) {
267271
super(createErrDiff(actual, expected, operator, message, diff));
268272
} else {
269273
super(String(message));
@@ -283,7 +287,7 @@ class AssertionError extends Error {
283287
expected = copyError(expected);
284288
}
285289

286-
if (kMethodsWithCustomMessageDiff.includes(operator)) {
290+
if (kMethodsWithCustomMessageDiff.has(operator)) {
287291
super(createErrDiff(actual, expected, operator, message, diff));
288292
} else if (operator === 'notDeepStrictEqual' ||
289293
operator === 'notStrictEqual') {

0 commit comments

Comments
 (0)