Run details:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36
Chrome 110
Linux
Desktop
2 years ago
Test name Executions per second
getElementById each time 175.3 Ops/sec
Own global variable 363.3 Ops/sec
Own global variable with equal name 347.1 Ops/sec
HTML5 global variable based on id value 86.3 Ops/sec
HTML Preparation code:
AخA
 
1
<table id="testTable">
2
</table>
Script Preparation code:
x
 
// some elemens without id
for(var i=0; i<5000; ++i)
  testTable.insertRow(-1).insertCell(-1).textContent = 'Row #'+i;
// some elements with id
var parent = testTable.parentElement;
for(var i=0; i<5000; ++i)
{
  var div = document.createElement('div');
  div.id = 'divBefore'+i;
  parent.insertBefore(div, testTable);
  
  div = document.createElement('div');
  div.id = 'divAfter'+i;
  parent.appendChild(div);
}
Tests:
  • getElementById each time

     
    for(var i=0; i<10000; ++i)
    {
        var table = document.getElementById('testTable');
        table.rows[0].cells[0].textContent = 'Test';
    }
  • Own global variable

     
    var table = document.getElementById('testTable');
    for(var i=0; i<10000; ++i)
    {
        table.rows[0].cells[0].textContent = 'Test';
    }
  • Own global variable with equal name

     
    var testTable = document.getElementById('testTable');
    for(var i=0; i<10000; ++i)
    {
        testTable.rows[0].cells[0].textContent = 'Test';
    }
  • HTML5 global variable based on id value

     
    for(var i=0; i<10000; ++i)
    {
        testTable.rows[0].cells[0].textContent = 'Test';
    }