assert: use a set instead of an array for faster lookup

This is a very small improvement for error creation.

PR-URL: https://github.com/nodejs/node/pull/61076
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
Ruben Bridgewater
2025-12-15 18:18:53 +01:00
committed by Node.js GitHub Bot
parent fc1d6d6b4c
commit 6157c81f4b

View File

@@ -10,6 +10,7 @@ const {
ObjectDefineProperty,
ObjectGetPrototypeOf,
ObjectPrototypeHasOwnProperty,
SafeSet,
String,
StringPrototypeRepeat,
StringPrototypeSlice,
@@ -42,7 +43,10 @@ const kReadableOperator = {
const kMaxShortStringLength = 12;
const kMaxLongStringLength = 512;
const kMethodsWithCustomMessageDiff = ['deepStrictEqual', 'strictEqual', 'partialDeepStrictEqual'];
const kMethodsWithCustomMessageDiff = new SafeSet()
.add('deepStrictEqual')
.add('strictEqual')
.add('partialDeepStrictEqual');
function copyError(source) {
const target = ObjectAssign(
@@ -263,7 +267,7 @@ class AssertionError extends Error {
if (isErrorStackTraceLimitWritable()) Error.stackTraceLimit = 0;
if (message != null) {
if (kMethodsWithCustomMessageDiff.includes(operator)) {
if (kMethodsWithCustomMessageDiff.has(operator)) {
super(createErrDiff(actual, expected, operator, message, diff));
} else {
super(String(message));
@@ -283,7 +287,7 @@ class AssertionError extends Error {
expected = copyError(expected);
}
if (kMethodsWithCustomMessageDiff.includes(operator)) {
if (kMethodsWithCustomMessageDiff.has(operator)) {
super(createErrDiff(actual, expected, operator, message, diff));
} else if (operator === 'notDeepStrictEqual' ||
operator === 'notStrictEqual') {