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

perf(NODE-6127): move error construction into setTimeout callback #4094

Merged
merged 2 commits into from
Apr 23, 2024
Merged
Changes from all commits
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
6 changes: 1 addition & 5 deletions src/timeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export class Timeout extends Promise<never> {
return 'MongoDBTimeout';
}

private timeoutError: TimeoutError;
private id?: NodeJS.Timeout;

public readonly start: number;
Expand All @@ -55,17 +54,14 @@ export class Timeout extends Promise<never> {
executor(noop, promiseReject);
});

// NOTE: Construct timeout error at point of Timeout instantiation to preserve stack traces
this.timeoutError = new TimeoutError(`Expired after ${duration}ms`);

this.duration = duration;
this.start = Math.trunc(performance.now());

if (this.duration > 0) {
this.id = setTimeout(() => {
this.ended = Math.trunc(performance.now());
this.timedOut = true;
reject(this.timeoutError);
reject(new TimeoutError(`Expired after ${duration}ms`));
}, this.duration);
// Ensure we do not keep the Node.js event loop running
if (typeof this.id.unref === 'function') {
Expand Down
Loading