From 6157c81f4b28a0448207e0c915e4d218c5fc30c3 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Mon, 15 Dec 2025 18:18:53 +0100 Subject: [PATCH] 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 Reviewed-By: Yagiz Nizipli Reviewed-By: Antoine du Hamel --- lib/internal/assert/assertion_error.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/internal/assert/assertion_error.js b/lib/internal/assert/assertion_error.js index 5c15b96b12..5dbf1e7a34 100644 --- a/lib/internal/assert/assertion_error.js +++ b/lib/internal/assert/assertion_error.js @@ -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') {