From 6abc074b3b1f2bbcc5d7252ab4cbfbd729fb4eb2 Mon Sep 17 00:00:00 2001 From: Warren James Date: Tue, 23 Apr 2024 18:00:27 -0400 Subject: [PATCH] perf(NODE-6127): move error construction into setTimeout callback (#4094) --- src/timeout.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/timeout.ts b/src/timeout.ts index 7191f981de..fadf9727a6 100644 --- a/src/timeout.ts +++ b/src/timeout.ts @@ -33,7 +33,6 @@ export class Timeout extends Promise { return 'MongoDBTimeout'; } - private timeoutError: TimeoutError; private id?: NodeJS.Timeout; public readonly start: number; @@ -55,9 +54,6 @@ export class Timeout extends Promise { 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()); @@ -65,7 +61,7 @@ export class Timeout extends Promise { 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') {