Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Class Selector vs ID Selector vs Data Attribute Selector Performance Test
(version: 0)
Class Selector vs ID Selector vs Data Attribute Selector Performance Test
Comparing performance of:
Class vs ID vs Data Attribute vs Data Attribute Exact
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div class="hello" id="hello" data-hello="some long long long long value" data-test="hello-and-bye">Hello</div> >Hello</div>
Tests:
Class
document.querySelector('.hello');
ID
document.querySelector('#hello');
Data Attribute
document.querySelector('[data-hello]');
Data Attribute Exact
document.querySelectorAll('[data-test="hello-and-bye"]')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Class
ID
Data Attribute
Data Attribute Exact
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Class
4274053.5 Ops/sec
ID
2973556.2 Ops/sec
Data Attribute
2866223.5 Ops/sec
Data Attribute Exact
2018064.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark measures the performance of three different approaches to selecting elements in an HTML document using JavaScript: 1. Class Selector 2. ID Selector 3. Data Attribute Selector (with exact matching) 4. Data Attribute Selector (without exact matching) **Approaches Compared** Here's a brief overview of each approach and their pros and cons: ### 1. Class Selector * **Description**: `document.querySelector('.hello');` * **Pros**: Fast and efficient, as class selectors are cached by browsers. * **Cons**: Can be slower for deeply nested or complex classes. ### 2. ID Selector * **Description**: `document.querySelector('#hello');` * **Pros**: Fast and efficient, as ID selectors are unique and easily lookable up. * **Cons**: May not work if multiple elements have the same ID (although this is rare). ### 3. Data Attribute Selector (with exact matching) * **Description**: `document.querySelector('[data-hello]');` * **Pros**: Flexible and powerful, as it can match any attribute with a specific value. * **Cons**: May be slower due to the need for attribute lookup. ### 4. Data Attribute Selector (without exact matching) * **Description**: `document.querySelectorAll('[data-test="hello-and-bye"]')` * **Pros**: Similar flexibility to the previous approach, but with less overhead. * **Cons**: May still incur some performance cost due to attribute lookup. **Library and Special Features** None of the test cases use any external libraries or special JavaScript features. However, it's worth noting that using `document.querySelectorAll()` can return an array of matching elements, which might affect performance depending on the context. **Device and Browser** The benchmark is run on a Chrome browser with version 126, executed on a Linux desktop platform, and has an estimated 427M executions per second. The device platform and operating system are specified as well. **Alternatives** Other alternatives for selecting elements in JavaScript include: * `document.getElementsByClassName()` (similar to class selector) * `document.getElementById()` (similar to ID selector) * Using CSS selectors with `getElementById()`, `getElementsByClassName()`, or `querySelectorAll()` However, using pure JavaScript element selection methods can be less efficient and may incur more overhead compared to the approaches tested in this benchmark. Keep in mind that these alternatives might not match the exact performance characteristics of the approaches being tested in this benchmark.
Related benchmarks:
DataAttribute vs Class Selector vs ID Selector
DataAttribute vs Class Selector vs ID native Selector
Class Selector vs ID Selector vs Extended Data Attribute Selector Performance Test
DataAttribute vs Class Selector vs ID Selector vs Custom Attributes
DataAttribute vs Class Selector vs ID Selector vs ~=
Comments
Confirm delete:
Do you really want to delete benchmark?