Skip to content

Commit ddd95b8

Browse files
authored
Merge pull request #15136 from Automattic/vkarpov15/gh-10449-2
perf(cursor): clear the stack every time if using populate with batchSize to avoid stack overflows with large docs
2 parents 494df86 + 5419077 commit ddd95b8

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

lib/cursor/queryCursor.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -557,17 +557,11 @@ function _onNext(error, doc) {
557557

558558
if (this.ctx._batchDocs.length < this.ctx.options._populateBatchSize) {
559559
// If both `batchSize` and `_populateBatchSize` are huge, calling `next()` repeatedly may
560-
// cause a stack overflow. So make sure we clear the stack regularly.
561-
if (this.ctx._batchDocs.length > 0 && this.ctx._batchDocs.length % 1000 === 0) {
562-
return immediate(() => this.ctx.cursor.next().then(
563-
res => { _onNext.call(this, null, res); },
564-
err => { _onNext.call(this, err); }
565-
));
566-
}
567-
this.ctx.cursor.next().then(
560+
// cause a stack overflow. So make sure we clear the stack.
561+
immediate(() => this.ctx.cursor.next().then(
568562
res => { _onNext.call(this, null, res); },
569563
err => { _onNext.call(this, err); }
570-
);
564+
));
571565
} else {
572566
_populateBatch.call(this);
573567
}

0 commit comments

Comments
 (0)