Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
getElementById vs HTML5 global variable based on id value fixed
(version: 0)
https://html.spec.whatwg.org/#named-access-on-the-window-object
Comparing performance of:
getElementById each time vs Own global variable vs Own global variable with equal name vs HTML5 global variable based on id value
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<table id="testTable"> </table>
Script Preparation code:
// some elemens without id for(var i=0; i<5000; ++i) testTable.insertRow(-1).insertCell(-1).textContent = 'Row #'+i; // some elements with id for(var i=0; i<5000; ++i) { var div = document.createElement('div'); div.id = 'divBefore'+i; testTable.parentElement.insertBefore(div, testTable); div = document.createElement('div'); div.id = 'divAfter'+i; testTable.parentElement.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'; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
getElementById each time
Own global variable
Own global variable with equal name
HTML5 global variable based on id value
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark and explore what's being tested. **Benchmark Definition** The benchmark defines four test cases: 1. `getElementById each time`: This test case uses `document.getElementById` to retrieve an element by its ID, performs some operations on it, and then repeats this process 10,000 times. 2. `Own global variable`: Similar to the first test case, but instead of using `document.getElementById`, it assigns the result to a global variable `table`. 3. `Own global variable with equal name`: Same as the second test case, but the global variable has the same name as the element's ID (`testTable`). 4. `HTML5 global variable based on id value`: This test case uses the new HTML5 feature, `named access on the window object`, to retrieve an element by its ID and perform operations on it. **Options Compared** The benchmark compares four different approaches: 1. **`document.getElementById`**: A traditional method of retrieving an element by its ID. 2. **Assigning result to a global variable (`table`)**: Stores the result in a global variable, which can lead to caching and performance implications. 3. **Assigning result to a local variable with equal name as the element's ID (`testTable`)**: Similar to the second approach, but with a local variable having the same name as the element's ID. 4. **HTML5 global variable based on id value**: Utilizes the new HTML5 feature to retrieve an element by its ID, which can provide better performance and caching. **Pros and Cons** Here are some pros and cons of each approach: 1. **`document.getElementById`**: * Pros: Simple and widely supported. * Cons: Can lead to slow performance due to DOM lookup. 2. **Assigning result to a global variable (`table`)**: * Pros: Can provide caching, reducing the number of DOM lookups. * Cons: May not be suitable for all use cases (e.g., when `table` is reassigned). 3. **Assigning result to a local variable with equal name as the element's ID (`testTable`)**: * Pros: Similar to the second approach, but with a local variable. * Cons: Same limitations as the second approach. 4. **HTML5 global variable based on id value**: * Pros: Provides better performance and caching due to named access. * Cons: Requires support for HTML5 features. **Other Considerations** When using `document.getElementById`, consider the following: * The document may not exist or be accessible when trying to retrieve an element by its ID. * The element's ID may change dynamically, affecting the accuracy of the benchmark. The `named access on the window object` feature (HTML5) is a more efficient way to retrieve elements by their ID. However, it requires careful consideration of compatibility and support for different browsers. **Alternatives** Other alternatives for retrieving elements by their ID include: * Using a library like jQuery or a custom implementation. * Utilizing a virtual DOM or a lightweight DOM library. * Employing a caching mechanism, such as memoization or a cache store. Keep in mind that each alternative has its pros and cons, which should be evaluated on a case-by-case basis.
Related benchmarks:
getElementById vs HTML5 global variable based on id value
GlobalVariable vs getElementById
Dynamic Create of Table: insertRow() vs appending HTML
Add rows to Table: insertRow() vs appending innerHTML vs insertAdjacentHTML
Comments
Confirm delete:
Do you really want to delete benchmark?