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.
  • Loading branch information
rayark1 committed Jul 23, 2024
1 parent 4174b73 commit 0d74220
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 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,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;
}
Expand Down

0 comments on commit 0d74220

Please sign in to comment.