From 11f1d884d08fd64b6a331bae6eedc56d8f7988aa Mon Sep 17 00:00:00 2001 From: Lucas Hansen Date: Fri, 6 Nov 2015 14:49:44 -0800 Subject: [PATCH] Added a minimongo optimization to avoid some unecessary clones --- packages/minimongo/minimongo.js | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/packages/minimongo/minimongo.js b/packages/minimongo/minimongo.js index 236a759629..f5e9a08863 100644 --- a/packages/minimongo/minimongo.js +++ b/packages/minimongo/minimongo.js @@ -696,12 +696,33 @@ LocalCollection.prototype.update = function (selector, mod, options, callback) { // it. (We don't need to save the original results of paused queries because // they already have a resultsSnapshot and we won't be diffing in // _recomputeResults.) - var qidToOriginalResults = {}; + var qidToOriginalResults = {}, + docMap = {}, // We should only clone each document once, even if it appears in multiple queries + onlyIDQuery = _.size(selector) === 1 && _.isString(selector._id); // If the selector is just looking up a single _id, then we only need to clone that document _.each(self.queries, function (query, qid) { // XXX for now, skip/limit implies ordered observe, so query.results is // always an array - if ((query.cursor.skip || query.cursor.limit) && ! self.paused) - qidToOriginalResults[qid] = EJSON.clone(query.results); + if ((query.cursor.skip || query.cursor.limit) && ! self.paused) { + + // This is to catch LocalCollection._IdMap + if (_.isUndefined(query.results.length)) { + qidToOriginalResults[qid] = EJSON.clone(query.results); + return; + } + + qidToOriginalResults[qid] = query.results.map(function(result) { + if (_.isUndefined(result) || _.isUndefined(result._id)) + return EJSON.clone(result); + + if (!_.has(docMap, result._id)) { + docMap[result._id] = (onlyIDQuery && result._id === selector._id) + ? EJSON.clone(result) + : result; + } + + return docMap[result._id]; + }); + } }); var recomputeQids = {};