Dom element id insertion

Benchmark created on


Description

check performance of DOM ID

Preparation HTML

<div id="xyz"></div>

Test runner

Ready to run.

Testing in
TestOps/sec
Without id
let parentDiv = document.getElementById('xyz');

// Loop 100 times
for (let i = 0; i < 100; i++) {
    // Create a new div element
    let newDiv = document.createElement('div');
    
    // Optionally, set some content or attributes for the new div
    newDiv.textContent = 'Div ' + (i + 1);
    
    // Append the new div to the parent div
    parentDiv.appendChild(newDiv);
}
ready
With id
// Get the parent div by its ID
let parentDiv = document.getElementById('xyz');

// Loop 100 times
for (let i = 0; i < 100; i++) {
    // Create a new div element
    let newDiv = document.createElement('div');
    
    // Set the text content
    newDiv.textContent = 'Div ' + (i + 1);
    
    // Set the id attribute
    newDiv.id = 'div-' + (i + 1);
    
    // Append the new div to the parent div
    parentDiv.appendChild(newDiv);
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.