Skip to content

Commit

Permalink
perf(NODE-6127): move error construction into setTimeout callback (#4094
Browse files Browse the repository at this point in the history
)
  • Loading branch information
W-A-James authored Apr 23, 2024
1 parent 3598c23 commit 6abc074
Showing 1 changed file with 1 addition and 5 deletions.
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

0 comments on commit 6abc074

Please sign in to comment.