From 546dab29c1b6e39f1f460bbe2a5fb620e2e26b20 Mon Sep 17 00:00:00 2001 From: HEESEUNG Date: Thu, 25 Jul 2024 21:12:35 +0900 Subject: [PATCH] lib: optimize copyError with ObjectAssign in primordials optimized the copyError function by using ObjectAssign from primordials. this change replaces the for-loop with ObjectAssign, which improves memory usage and performance. this change updates the copyError function in internal/assert.js to use ObjectAssign for copying properties. PR-URL: https://github.com/nodejs/node/pull/53999 Reviewed-By: James M Snell Reviewed-By: Antoine du Hamel Reviewed-By: Daeyeon Jeong --- lib/internal/assert/assertion_error.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/internal/assert/assertion_error.js b/lib/internal/assert/assertion_error.js index f12243790b0506..5d8c45040af0fe 100644 --- a/lib/internal/assert/assertion_error.js +++ b/lib/internal/assert/assertion_error.js @@ -6,9 +6,9 @@ const { Error, ErrorCaptureStackTrace, MathMax, + ObjectAssign, ObjectDefineProperty, ObjectGetPrototypeOf, - ObjectKeys, String, StringPrototypeEndsWith, StringPrototypeRepeat, @@ -46,11 +46,7 @@ const kReadableOperator = { const kMaxShortLength = 12; function copyError(source) { - const keys = ObjectKeys(source); - const target = { __proto__: ObjectGetPrototypeOf(source) }; - for (const key of keys) { - target[key] = source[key]; - } + const target = ObjectAssign({ __proto__: ObjectGetPrototypeOf(source) }, source); ObjectDefineProperty(target, 'message', { __proto__: null, value: source.message }); return target; }