Skip to content

Commit

Permalink
lib: optimize copyError with ObjectAssign in primordials
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
rayark1 authored and marco-ippolito committed Aug 19, 2024
1 parent bfabfb4 commit 0d70c79
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions lib/internal/assert/assertion_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const {
Error,
ErrorCaptureStackTrace,
MathMax,
ObjectAssign,
ObjectDefineProperty,
ObjectGetPrototypeOf,
ObjectKeys,
String,
StringPrototypeEndsWith,
StringPrototypeRepeat,
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 0d70c79

Please sign in to comment.