minimize dom access javascript

Solutions on MaxInterview for minimize dom access javascript by the best coders in the world

showing results for - "minimize dom access javascript"
Sara
13 May 2019
1// This is slow, it accesses the DOM element multiple times
2document.querySelector('#my-element').classList.add('my-class');
3document.querySelector('#my-element').textContent = 'hello';
4document.querySelector('#my-element').hidden = false;
5
6// This is faster, it stores the DOM element in a variable
7const myElement = document.querySelector('#my-element');
8myElement.classList.add('my-class');
9myElement.textContent = 'hello';
10myElement.hidden = false;