Răsfoiți Sursa

Update all for/in loops to standard for loops for performance

Lee Morgan 6 ani în urmă
părinte
comite
9d9a4c1217

+ 4 - 4
controllers/merchantData.js

@@ -79,12 +79,12 @@ module.exports = {
                 axios.get(`${process.env.CLOVER_ADDRESS}/v3/merchants/${req.session.merchantId}/items?access_token=${req.session.accessToken}`)
                     .then((response)=>{
                         let recipes = [];
-                        for(let item of response.data.elements){
+                        for(let i = 0; i < response.data.elements.length; i++){}
                             let recipe = new Recipe({
-                                posId: item.id,
+                                posId: response.data.elements[i].id,
                                 merchant: merchant,
-                                name: item.name,
-                                price: item.price,
+                                name: response.data.elements[i].name,
+                                price: response.data.elements[i].price,
                                 ingredients: []
                             });
 

+ 5 - 5
controllers/otherData.js

@@ -58,11 +58,11 @@ module.exports = {
         let authorizationCode = "";
         let merchantId = "";
 
-        for(let str of dataArr){
-            if(str.slice(0, str.indexOf("=")) === "merchant_id"){
-                merchantId = str.slice(str.indexOf("=") + 1);
-            }else if(str.slice(0, str.indexOf("=")) === "code"){
-                authorizationCode = str.slice(str.indexOf("=") + 1);
+        for(let i = 0; i < dataArr.length; i++){
+            if(dataArr[i].slice(0, dataArr[i].indexOf("=")) === "merchant_id"){
+                merchantId = dataArr[i].slice(dataArr[i].indexOf("=") + 1);
+            }else if(dataArr[i].slice(0, dataArr[i].indexOf("=")) === "code"){
+                authorizationCode = dataArr[i].slice(dataArr[i].indexOf("=") + 1);
             }
         }
 

+ 8 - 8
controllers/recipeData.js

@@ -175,23 +175,23 @@ module.exports = {
                             }
                         }
 
-                        for(let recipe of deletedRecipes){
-                            for(let i = 0; i < merchant.recipes.length; i++){
-                                if(recipe._id === merchant.recipes[i]._id){
-                                    merchant.recipes.splice(i, 1);
+                        for(let i = 0; i < deletedRecipes.length; i++){
+                            for(let j = 0; j < merchant.recipes.length; j++){
+                                if(deletedRecipes[i]._id === merchant.recipes[j]._id){
+                                    merchant.recipes.splice(j, 1);
                                     break;
                                 }
                             }
                         }
 
                         let newRecipes = []
-                        for(let recipe of result.data.elements){
+                        for(let i = 0; i < result.data.elements.length; i++){
                             let newRecipe = new Recipe({
-                                posId: recipe.id,
+                                posId: result.data.elements[i].id,
                                 merchant: merchant._id,
-                                name: recipe.name,
+                                name: result.data.elements[i].name,
                                 ingredients: [],
-                                price: recipe.price
+                                price: result.data.elements[i].price
                             });
 
                             merchant.recipes.push(newRecipe);

+ 18 - 18
views/dashboardPage/bundle.js

@@ -953,8 +953,8 @@ controller = {
         if(!menu.classList.contains("menuMinimized")){
             menu.classList = "menu menuMinimized";
 
-            for(let button of buttons){
-                button.children[1].style.display = "none";
+            for(let i = 0; i < buttons.length; i++){
+                buttons[i].children[1].style.display = "none";
             }
 
             document.getElementById("max").style.display = "none";
@@ -964,8 +964,8 @@ controller = {
         }else if(menu.classList.contains("menuMinimized")){
             menu.classList = "menu";
 
-            for(let button of buttons){
-                button.children[1].style.display = "block";
+            for(let i = 0; i < buttons.length; i++){
+                buttons[i].children[1].style.display = "block";
             }
 
             setTimeout(()=>{
@@ -1864,15 +1864,15 @@ module.exports = {
             ingredientsSelect.removeChild(ingredientsSelect.firstChild);
         }
 
-        for(let category of categories){
+        for(let i = 0; i < categories.length; i++){
             let optgroup = document.createElement("optgroup");
-            optgroup.label = category.name;
+            optgroup.label = categories[i].name;
             ingredientsSelect.appendChild(optgroup);
 
-            for(let ingredient of category.ingredients){
+            for(let j = 0; j < categories[i].ingredients.length; j++){
                 let option = document.createElement("option");
-                option.value = ingredient.ingredient.id;
-                option.innerText = `${ingredient.ingredient.name} (${ingredient.ingredient.unit})`;
+                option.value = categories[i].ingredients[j].ingredient.id;
+                option.innerText = `${categories[i].ingredients[j].ingredient.name} (${categories[i].ingredients[j].ingredient.unit})`;
                 optgroup.appendChild(option);
             }
         }
@@ -2881,9 +2881,9 @@ class LineGraph{
         this.data.push(data);
 
         let isChange = false;
-        for(let point of data.set){
-            if(point > this.max){
-                this.max = point;
+        for(let i = 0; i < data.set.length; i++){
+            if(data.set[i] > this.max){
+                this.max = data.set[i];
                 this.verticalMultiplier = (this.bottom - this.top) / this.max;
                 this.horizontalMultiplier = (this.right - this.left) / (data.set.length - 1);
                 isChange = true;
@@ -2948,8 +2948,8 @@ class LineGraph{
         this.drawYAxis();
         this.drawXAxis();
 
-        for(let dataSet of this.data){
-            this.drawLine(dataSet);
+        for(let i = 0; i < this.data.length; i++){
+            this.drawLine(this.data[i]);
         }
 
         if(this.title){
@@ -3085,12 +3085,12 @@ class HorizontalBarGraph{
     addData(dataArray){
         this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
 
-        for(let point of dataArray){
-            if(point.num > this.max){
-                this.max = point.num;
+        for(let i = 0; i < dataArray.length; i++){
+            if(dataArray[i].num > this.max){
+                this.max = dataArray[i].num;
             }
 
-            this.data.push(point);
+            this.data.push(dataArray[i]);
         }
 
         this.drawGraph();

+ 4 - 4
views/dashboardPage/js/dashboard.js

@@ -142,8 +142,8 @@ controller = {
         if(!menu.classList.contains("menuMinimized")){
             menu.classList = "menu menuMinimized";
 
-            for(let button of buttons){
-                button.children[1].style.display = "none";
+            for(let i = 0; i < buttons.length; i++){
+                buttons[i].children[1].style.display = "none";
             }
 
             document.getElementById("max").style.display = "none";
@@ -153,8 +153,8 @@ controller = {
         }else if(menu.classList.contains("menuMinimized")){
             menu.classList = "menu";
 
-            for(let button of buttons){
-                button.children[1].style.display = "block";
+            for(let i = 0; i < buttons.length; i++){
+                buttons[i].children[1].style.display = "block";
             }
 
             setTimeout(()=>{

+ 5 - 5
views/dashboardPage/js/newRecipe.js

@@ -8,15 +8,15 @@ module.exports = {
             ingredientsSelect.removeChild(ingredientsSelect.firstChild);
         }
 
-        for(let category of categories){
+        for(let i = 0; i < categories.length; i++){
             let optgroup = document.createElement("optgroup");
-            optgroup.label = category.name;
+            optgroup.label = categories[i].name;
             ingredientsSelect.appendChild(optgroup);
 
-            for(let ingredient of category.ingredients){
+            for(let j = 0; j < categories[i].ingredients.length; j++){
                 let option = document.createElement("option");
-                option.value = ingredient.ingredient.id;
-                option.innerText = `${ingredient.ingredient.name} (${ingredient.ingredient.unit})`;
+                option.value = categories[i].ingredients[j].ingredient.id;
+                option.innerText = `${categories[i].ingredients[j].ingredient.name} (${categories[i].ingredients[j].ingredient.unit})`;
                 optgroup.appendChild(option);
             }
         }

+ 4 - 4
views/shared/banner.ejs

@@ -37,18 +37,18 @@
                 ul.removeChild(ul.firstChild);
             }
 
-            for(let notification of this.notificationList){
+            for(let i = 0; i < this.notificationList.length; i++){
                 let li = document.createElement("li");                                                                       
                                                                                                             
                 li.classList = "notification";
-                li.innerText = notification;
+                li.innerText = this.notificationList[i];
                 ul.appendChild(li);
             }
 
-            for(let error of this.errorList){
+            for(let i = 0; i < this.errorList.length; i++){
                 let li = document.createElement("li");
                 li.classList = "error";
-                li.innerText = error;
+                li.innerText = this.errorList[i];
                 ul.appendChild(li);
             }
         }

+ 9 - 9
views/shared/graphs.js

@@ -43,9 +43,9 @@ class LineGraph{
         this.data.push(data);
 
         let isChange = false;
-        for(let point of data.set){
-            if(point > this.max){
-                this.max = point;
+        for(let i = 0; i < data.set.length; i++){
+            if(data.set[i] > this.max){
+                this.max = data.set[i];
                 this.verticalMultiplier = (this.bottom - this.top) / this.max;
                 this.horizontalMultiplier = (this.right - this.left) / (data.set.length - 1);
                 isChange = true;
@@ -110,8 +110,8 @@ class LineGraph{
         this.drawYAxis();
         this.drawXAxis();
 
-        for(let dataSet of this.data){
-            this.drawLine(dataSet);
+        for(let i = 0; i < this.data.length; i++){
+            this.drawLine(this.data[i]);
         }
 
         if(this.title){
@@ -247,12 +247,12 @@ class HorizontalBarGraph{
     addData(dataArray){
         this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
 
-        for(let point of dataArray){
-            if(point.num > this.max){
-                this.max = point.num;
+        for(let i = 0; i < dataArray.length; i++){
+            if(dataArray[i].num > this.max){
+                this.max = dataArray[i].num;
             }
 
-            this.data.push(point);
+            this.data.push(dataArray[i]);
         }
 
         this.drawGraph();

+ 11 - 11
views/shared/validation.js

@@ -97,8 +97,8 @@ let validator = {
 
             if(errors.length > 0){
                 if(createBanner){
-                    for(let error of errors){
-                        banner.createError(error);
+                    for(let i = 0; i < errors.length; i++){
+                        banner.createError(errors[i]);
                     }
 
                     return false;
@@ -125,13 +125,13 @@ let validator = {
         }
 
         let checkSet = new Set();
-        for(let ingredient of newRecipe.ingredients){
-            if(ingredient.quantity < 0){
+        for(let i = 0; i < newRecipe.ingredients.length; i++){
+            if(newRecipe.ingredients[i].quantity < 0){
                 errors.push("Quantity must contain a non-negative number");
                 break;
             }
 
-            checkSet.add(ingredient.ingredient);
+            checkSet.add(newRecipe.ingredients[i].ingredient);
         }
 
         if(checkSet.size !== newRecipe.ingredients.length){
@@ -144,8 +144,8 @@ let validator = {
 
         if(errors.length > 0){
             if(createBanner){
-                for(let error of errors){
-                    banner.createError(error);
+                for(let i = 0; i < errors.length; i++){
+                    banner.createError(errors[i]);
                 }
 
                 return false;
@@ -185,8 +185,8 @@ let validator = {
 
         if(errors.length > 0){
             if(createBanner){
-                for(let error of errors){
-                    banner.createError(error);
+                for(let i = 0; i < errors.length; i++){
+                    banner.createError(errors[i]);
                 }
             }
 
@@ -199,8 +199,8 @@ let validator = {
     isSanitary: function(str, createBanner = true){
         let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
 
-        for(let char of disallowed){
-            if(str.includes(char)){
+        for(let i = 0; i < disallowed.length; i++){
+            if(str.includes(disallowed[i])){
                 if(createBanner){
                     banner.createError("Your string contains illegal characters");
                 }