Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

errors: improve error creation performance #46648

Closed
wants to merge 15 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
errors: improve hideInternalStackFrames performance
Directly concat the necessary information to a string instead of
allocating intermediate arrays and iterating over the array more
than once.

Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
BridgeAR committed Mar 2, 2023
commit ed1a66430910036e6c47328e00811eeceeae611f
34 changes: 19 additions & 15 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const {
AggregateError,
ArrayFrom,
ArrayIsArray,
ArrayPrototypeFilter,
ArrayPrototypeIncludes,
ArrayPrototypeIndexOf,
ArrayPrototypeJoin,
Expand Down Expand Up @@ -89,18 +88,18 @@ const nodeInternalPrefix = '__node_internal_';
const prepareStackTrace = (globalThis, error, trace) => {
// API for node internals to override error stack formatting
// without interfering with userland code.
if (overrideStackTrace.has(error)) {
const f = overrideStackTrace.get(error);
const fn = overrideStackTrace.get(error);
if (fn !== undefined) {
overrideStackTrace.delete(error);
return f(error, trace);
return fn(error, trace);
}

const firstFrame = trace[0]?.getFunctionName();
if (firstFrame && StringPrototypeStartsWith(firstFrame, nodeInternalPrefix)) {
for (let l = trace.length - 1; l >= 0; l--) {
const fn = trace[l]?.getFunctionName();
for (let i = trace.length - 1; i >= 0; i--) {
const fn = trace[i]?.getFunctionName();
if (fn && StringPrototypeStartsWith(fn, nodeInternalPrefix)) {
ArrayPrototypeSplice(trace, 0, l + 1);
ArrayPrototypeSplice(trace, 0, i + 1);
break;
}
}
Expand Down Expand Up @@ -828,16 +827,21 @@ function setArrowMessage(err, arrowMessage) {
// Hide stack lines before the first user code line.
function hideInternalStackFrames(error) {
overrideStackTrace.set(error, (error, stackFrames) => {
let frames = stackFrames;
let result = '';
if (typeof stackFrames === 'object') {
frames = ArrayPrototypeFilter(
stackFrames,
(frm) => !StringPrototypeStartsWith(frm.getFileName() || '',
'node:internal'),
);
for (const frame of stackFrames) {
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
const filename = frame.getFileName();
if (!filename || !StringPrototypeStartsWith(filename, 'node:internal')) {
result += `\n at ${frame}`;
}
}
} else {
for (const frame of stackFrames) {
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
result += `\n at ${frame}`;
}
}
ArrayPrototypeUnshift(frames, error);
return ArrayPrototypeJoin(frames, '\n at ');
result = error + result;
return result;
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
});
}

Expand Down