DEV Community

Fernando Marques
Fernando Marques

Posted on

Performance: Create a new variable or reassign the value?

The idea is to manipulate CSS elements. Which option would be the best performance? Is there any noticeable difference?

Top comments (3)

Collapse
 
deexter profile image
deexter

Could you provide some example? I am not sure what do you mean

Collapse
 
fernandorpm profile image
Fernando Marques

Hello!
For example, in order to assign a property to a CSS element via Javascript:

  let all = document.getElementsByClassName('class1');
  for (let i = 0; i < all.length; i++) {
    all[i].style.transitionDuration = time + 's';
  }
// reassign previous variable
  all = document.getElementsByClassName('class2');
  for (let i = 0; i < all.length; i++) {
    all[i].style.transitionDuration = time + 's';
  }
// OR
// create another variable
  let all_2 = document.getElementsByClassName('class3'); 
  for (let i = 0; i < all_2.length; i++) {
    all_2[i].style.transitionDuration = time + 's';
  }

Enter fullscreen mode Exit fullscreen mode

Is the difference even relevant?

Collapse
 
deexter profile image
deexter

I think performance meant in time spent to do this operation will be same, but memory consumption will be higher in last example.