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

improve performance of assignRawDocsToIdStructure #12867

Merged
merged 1 commit into from Jan 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 14 additions & 3 deletions lib/helpers/populate/assignRawDocsToIdStructure.js
Expand Up @@ -6,6 +6,8 @@ const utils = require('../../utils');

module.exports = assignRawDocsToIdStructure;

const kHasArray = Symbol('assignRawDocsToIdStructure.hasArray');

/**
* Assign `vals` returned by mongo query to the `rawIds`
* structure returned from utils.getVals() honoring
Expand Down Expand Up @@ -43,7 +45,16 @@ function assignRawDocsToIdStructure(rawIds, resultDocs, resultOrder, options, re

let i = 0;
const len = rawIds.length;
const hasResultArrays = Object.values(resultOrder).find(o => Array.isArray(o));

if (sorting && recursed && options[kHasArray] === undefined) {
options[kHasArray] = false;
for (const key in resultOrder) {
if (Array.isArray(resultOrder[key])) {
options[kHasArray] = true;
break;
}
}
}

for (i = 0; i < len; ++i) {
id = rawIds[i];
Expand All @@ -55,7 +66,7 @@ function assignRawDocsToIdStructure(rawIds, resultDocs, resultOrder, options, re
continue;
}

if (id === null && !sorting) {
if (id === null && sorting === false) {
// keep nulls for findOne unless sorting, which always
// removes them (backward compat)
newOrder.push(id);
Expand All @@ -79,7 +90,7 @@ function assignRawDocsToIdStructure(rawIds, resultDocs, resultOrder, options, re
if (doc) {
if (sorting) {
const _resultOrder = resultOrder[sid];
if (hasResultArrays) {
if (options[kHasArray]) {
// If result arrays, rely on the MongoDB server response for ordering
newOrder.push(doc);
} else {
Expand Down