Skip to content

Commit 0d70c79

Browse files
rayark1marco-ippolito
authored andcommitted
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: #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>
1 parent bfabfb4 commit 0d70c79

File tree

1 file changed

+2
-6
lines changed

1 file changed

+2
-6
lines changed

lib/internal/assert/assertion_error.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ const {
66
Error,
77
ErrorCaptureStackTrace,
88
MathMax,
9+
ObjectAssign,
910
ObjectDefineProperty,
1011
ObjectGetPrototypeOf,
11-
ObjectKeys,
1212
String,
1313
StringPrototypeEndsWith,
1414
StringPrototypeRepeat,
@@ -46,11 +46,7 @@ const kReadableOperator = {
4646
const kMaxShortLength = 12;
4747

4848
function copyError(source) {
49-
const keys = ObjectKeys(source);
50-
const target = { __proto__: ObjectGetPrototypeOf(source) };
51-
for (const key of keys) {
52-
target[key] = source[key];
53-
}
49+
const target = ObjectAssign({ __proto__: ObjectGetPrototypeOf(source) }, source);
5450
ObjectDefineProperty(target, 'message', { __proto__: null, value: source.message });
5551
return target;
5652
}

0 commit comments

Comments
 (0)