Will the efficient use of variables affect performance?

Tell us what’s happening:
Describe your issue in detail here.
Hey, I solved this challenge without defining any new variable. My question is, will this answer make a better performance than the one using new variable? If it doesn’t, is there any difference at all? Thanks!
Your code so far


function largestOfFour(arr) {
for(let i=0; i<arr.length; i++){
  for(let j=1; j<arr[i].length; j++){
    if(arr[i][j] > arr[i][0]){
      arr[i][0] = arr[i][j];
    }
  }
  arr[0][i] = arr[i][0];
}
return arr[0];
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

It’s generally a bad idea to change the input data unless you are explicitly clear that the purpose of the function is to modify the input array.

In this case, there is not really a significant benefit to this instead of declaring a new array of length 4, and it has the downside of modifying the input when that’s not the primary purpose of the function, so I wouldn’t do it.

3 Likes

There is always debate about the usefulness of avoiding unnecessary variables. And you’re right, it is good to be aware that its a thing.

But as has been said, you’re mutating the original array rather then creating a variable…and that’s swapping a worse practice for a questionable one.

An approach to consider later on, when you’ve learned array.map():

const largestOfFour = (arr) => {
  return arr.map( // here, use a math 
    // function to return the largest value
    //  in each nested array. This becomes
    //  the mapped value, placed in the final
    //  returned array. With no variables.
  );
}

array.map() is very useful, and creates a new array each time rather than mutate the input.

Now as to your original question, whether it is an efficiency hit to use variables. It really depends on the language -SQL you would be absolutely right, creating and populating variables in T-SQL is far less efficient. In javascript, unless you’re creating hundreds of thousands of variables, it’s generally no more or less efficient.

2 Likes

And most beginners (and some experienced people) worry too much about efficiency. There is the old programming adage, “premature optimization is the root of all evil.” Coders worry about optimization in the wrong places or in ways that don’t really affect things that much. Furthermore, in web dev, 99.99% of the time the app is just waiting for user interaction.

I wouldn’t worry about it too much, especially in one off things like this. This can get important if it is a data structure that gets repeated a few billion times or some function that gets called a few billion times.

In general, I say to make smart choices, but don’t worry about optimization until it is necessary.

Now, I would say that being aware of it is good. And later when you study things like Big-O notation you’ll get some tools to understand measuring algorithm efficiency, where (in some case) optimization can make a big difference.

In general, if adding a variable makes my code easier to read, I won’t hesitate. 99.999999% of the time, the performance affect won’t even approach being noticeable, not even close.

3 Likes

Personally, I think you should always leave parameters alone. They should only provide the inputs to the function and shouldn’t be overwritten. Use the values, but don’t overwrite them. It doesn’t matter if they are holding simple values or references.

Overwriting parameters can get confusing and it’s also likely that the variable name will not accurately reflect what the variable contains if you change the value. If they can the variable name might be too generic.

Code legibility is in almost all cases more important than optimization, especially when the performance optimization is premature.

2 Likes

Yeah, I forgot to comment on it, but yeah, I think mutating parameters is a very questionable idea. There might be some edge cases where the function is explicitly supposed to mutate and object or array (although there is almost certainly an immutable solution), mutating them in a function where one would not expect that (like here) would be a big red flag for me.

There is a principle in functional programming of a “pure function”. One of the characteristics is that it has no side-effects. Mutating input parameters is not only a side-effect, but it would be completely unexpected side-effect.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.