From 0d7422013d504ec1d4da6775803dd73008e8369e Mon Sep 17 00:00:00 2001 From: rayark1 Date: Tue, 23 Jul 2024 09:53:11 +0900 Subject: [PATCH 1/2] 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. --- lib/internal/assert/assertion_error.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/internal/assert/assertion_error.js b/lib/internal/assert/assertion_error.js index f12243790b0506..a3d335aa01b701 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,8 @@ 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]; - } + ObjectAssign(target, source); ObjectDefineProperty(target, 'message', { __proto__: null, value: source.message }); return target; } From 8b70d369b7c491fa7d18687182432c8a8340e36a Mon Sep 17 00:00:00 2001 From: rayark1 Date: Tue, 23 Jul 2024 16:32:47 +0900 Subject: [PATCH 2/2] lib: combine ObjectAssign into a single statement in copyError Changed the assignment of 'target' to use a single ObjectAssign statement. --- lib/internal/assert/assertion_error.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/internal/assert/assertion_error.js b/lib/internal/assert/assertion_error.js index a3d335aa01b701..5d8c45040af0fe 100644 --- a/lib/internal/assert/assertion_error.js +++ b/lib/internal/assert/assertion_error.js @@ -46,8 +46,7 @@ const kReadableOperator = { const kMaxShortLength = 12; function copyError(source) { - const target = { __proto__: ObjectGetPrototypeOf(source) }; - ObjectAssign(target, source); + const target = ObjectAssign({ __proto__: ObjectGetPrototypeOf(source) }, source); ObjectDefineProperty(target, 'message', { __proto__: null, value: source.message }); return target; }