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 df7a67c
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions lib/internal/assert/assertion_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
MathMax,
ObjectDefineProperty,
ObjectGetPrototypeOf,
ObjectAssign,

Check failure on line 11 in lib/internal/assert/assertion_error.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Out of ASCIIbetical order - ObjectGetPrototypeOf >= ObjectAssign
ObjectKeys,

Check failure on line 12 in lib/internal/assert/assertion_error.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'ObjectKeys' is assigned a value but never used
String,
StringPrototypeEndsWith,
Expand Down Expand Up @@ -46,11 +47,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 df7a67c

Please sign in to comment.