From e2ddaaa4aee2a393aafa869aaa6c28deac7fcb15 Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Sun, 26 Oct 2014 11:43:47 +0000 Subject: [PATCH] Refactors advanced optimizer to get rid of functions in loops. --- lib/selectors/optimizers/advanced.js | 50 +++++++++++++++++----------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/lib/selectors/optimizers/advanced.js b/lib/selectors/optimizers/advanced.js index d1da9958..d05d3f81 100644 --- a/lib/selectors/optimizers/advanced.js +++ b/lib/selectors/optimizers/advanced.js @@ -131,21 +131,24 @@ AdvancedOptimizer.prototype.reduceNonAdjacent = function (tokens) { AdvancedOptimizer.prototype.reduceSimpleNonAdjacentCases = function (tokens, repeated, candidates) { var reduced = false; + function filterOut(idx, bodies) { + return data[idx].isPartial && bodies.length === 0; + } + + function reduceBody(token, newBody, processedCount, tokenIdx) { + if (!data[processedCount - tokenIdx - 1].isPartial) { + changeBodyOf(token, newBody); + reduced = true; + } + } + for (var i = 0, l = repeated.length; i < l; i++) { var selector = repeated[i]; var data = candidates[selector]; - /* jshint loopfunc: true */ this.reduceSelector(tokens, selector, data, { - filterOut: function (idx, bodies) { - return data[idx].isPartial && bodies.length === 0; - }, - callback: function (token, newBody, processedCount, tokenIdx) { - if (!data[processedCount - tokenIdx - 1].isPartial) { - changeBodyOf(token, newBody); - reduced = true; - } - } + filterOut: filterOut, + callback: reduceBody }); } @@ -154,6 +157,16 @@ AdvancedOptimizer.prototype.reduceSimpleNonAdjacentCases = function (tokens, rep AdvancedOptimizer.prototype.reduceComplexNonAdjacentCases = function (tokens, candidates) { var reduced = false; + var localContext = {}; + + function filterOut(idx) { + return localContext.data[idx].where < localContext.intoPosition; + } + + function collectReducedBodies(token, newBody, processedCount, tokenIdx) { + if (tokenIdx === 0) + localContext.reducedBodies.push(newBody); + } allSelectors: for (var complexSelector in candidates) { @@ -163,11 +176,14 @@ AdvancedOptimizer.prototype.reduceComplexNonAdjacentCases = function (tokens, ca var intoPosition = into[into.length - 1].where; var intoToken = tokens[intoPosition]; + var reducedBodies = []; var selectors = this.isSpecial(complexSelector) ? [complexSelector] : into[0].list; - var reducedBodies = []; + + localContext.intoPosition = intoPosition; + localContext.reducedBodies = reducedBodies; for (var j = 0, m = selectors.length; j < m; j++) { var selector = selectors[j]; @@ -176,15 +192,11 @@ AdvancedOptimizer.prototype.reduceComplexNonAdjacentCases = function (tokens, ca if (data.length < 2) continue allSelectors; - /* jshint loopfunc: true */ + localContext.data = data; + this.reduceSelector(tokens, selector, data, { - filterOut: function (idx) { - return data[idx].where < intoPosition; - }, - callback: function (token, newBody, processedCount, tokenIdx) { - if (tokenIdx === 0) - reducedBodies.push(newBody); - } + filterOut: filterOut, + callback: collectReducedBodies }); if (reducedBodies[reducedBodies.length - 1].list.length != reducedBodies[0].list.length) -- 2.34.1