skip to Main Content

Scenario:

  • I have a 2D array which represents background colours of cells in a spreadsheet object:
[
 ["#FFFF00", "#FFFF00", "#FFFF00", "#FFFF00", "#FFFF00", "#FFFF00"],
 ["", "", "", "", "", ""],
 ["#FFFF00", "#FFFF00", "#FFFF00", "#FFFF00", "#FFFF00", "#FFFF00"],
 ["#FFFF00", "#FFFF00", "#FFFF00", "#FFFF00", "#FFFF00", "#FFFF00"],
 ["#FFFF00", "#FFFF00", "#FFFF00", "#FFFF00", "#FFFF00", "#FFFF00"],
 ["", "", "", "", "", ""],
 // etc
] 
  • The colour of the row is dependent on some if condition and can only be either all #FFFF00 or all "".
  • The colour of each cell has to be defined individually but the reality is the number of columns will always be the same for each row, and the colour across a row is uniform

Question: what would be the most efficient way of pushing each row of colours to the master array? Either creating the arrays outside the loop like this:

const yellowArr = ["#FFFF00", "#FFFF00", "#FFFF00", "#FFFF00", "#FFFF00", "#FFFF00"]
const emptyArr = ["", "", "", "", "", ""]

const masterArr = []

rows.forEach(function(row) [
  if (condition) {
    masterArr.push(yellowArr)
  }
  else {
    masterArr.push(emptyArr)
  }
])

Or simply pushing them like this within the loop:

const masterArr = []

rows.forEach(function(row) [
  if (condition) {
    masterArr.push(["#FFFF00", "#FFFF00", "#FFFF00", "#FFFF00", "#FFFF00", "#FFFF00"])
  }
  else {
    masterArr.push(["", "", "", "", "", ""])
  }
])

This is one part of a larger piece of code and the number of rows can be in the 10s of thousands so optimisation is of importance as the script has a limited time in which it can run.

Please feel free to reroute to a better suited stack exchange site, I was unable to find one specifically for code efficiency.

2

Answers


  1. You don’t necessarily have to create the yellowArr and emptyArr arrays outside the loop. We can check them via condition, no?

    I would be using only one loop to iterate through the rows and cells, then will push the color directly in master array.

    const masterArr = [];
    
    for (let i = 0; i < rows.length; i++) {
      masterArr.push([]);
    
      const color = condition ? "#FFFF00" : "";
    
      for (let j = 0; j < rows[i].length; j++) {
        masterArr[i].push(color);
      }
    }
    
    Login or Signup to reply.
  2. If you push an array to some other array, in your case masterArr, The reference of the array being pushed is pushed to the masterArr.

    Keeping this in mind, let’s see why your second option has some code that is problematic.

    masterArr.push(["#FFFF00", "#FFFF00", "#FFFF00", "#FFFF00", "#FFFF00", "#FFFF00"])
    
    masterArr.push(["", "", "", "", "", ""])
    

    These arrays are not assigned to any variable. Still, They will consume some memory in the heap. So, In each iteration of rows, new arrays will be created and memory will be used for the duplicate arrays. Which is not so memory efficient.

    Hence, Your first option is more memory efficient. Because the same memory reference will be pushed to masterArr array.

    But remember that if you make changes in yellowArr or emptyArr these arrays, then the same change will reflect in masterArr.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search