Refactors advanced optimizer to get rid of functions in loops.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Sun, 26 Oct 2014 11:43:47 +0000 (11:43 +0000)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Mon, 8 Dec 2014 09:39:14 +0000 (09:39 +0000)
lib/selectors/optimizers/advanced.js

index d1da995..d05d3f8 100644 (file)
@@ -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)