From 54190771bc45b1533fc2c8da11a957235a90c85e Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Fri, 27 Dec 2024 16:51:34 -0500 Subject: [PATCH] perf(cursor): clear the stack every time if using populate with batchSize to avoid stack overflows with large docs --- lib/cursor/queryCursor.js | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/lib/cursor/queryCursor.js b/lib/cursor/queryCursor.js index 5d05868f59..f25a06f2fd 100644 --- a/lib/cursor/queryCursor.js +++ b/lib/cursor/queryCursor.js @@ -557,17 +557,11 @@ function _onNext(error, doc) { if (this.ctx._batchDocs.length < this.ctx.options._populateBatchSize) { // If both `batchSize` and `_populateBatchSize` are huge, calling `next()` repeatedly may - // cause a stack overflow. So make sure we clear the stack regularly. - if (this.ctx._batchDocs.length > 0 && this.ctx._batchDocs.length % 1000 === 0) { - return immediate(() => this.ctx.cursor.next().then( - res => { _onNext.call(this, null, res); }, - err => { _onNext.call(this, err); } - )); - } - this.ctx.cursor.next().then( + // cause a stack overflow. So make sure we clear the stack. + immediate(() => this.ctx.cursor.next().then( res => { _onNext.call(this, null, res); }, err => { _onNext.call(this, err); } - ); + )); } else { _populateBatch.call(this); }