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 <jasnell@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
This commit is contained in:
HEESEUNG 2024-07-25 21:12:35 +09:00 committed by GitHub
parent 852a1a6742
commit 87e18a7b37
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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;
}