Run details:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36
Chrome 89
Windows
Desktop
4 years ago
Test name Executions per second
getElementById each time 125.4 Ops/sec
Own global variable 268.7 Ops/sec
Own global variable with equal name 271.3 Ops/sec
HTML5 global variable based on id value 74.2 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';
    }