<div id="wrap">
<div class="hello class2" id="hello2" data-src="weqwesad">Hello</div>
<img src="" width="500" height="600" class="hello class3" id="hello3" data-src="wedqwesd">
<div class="hello class4" id="hello4" data-src="weqwecsd">Hello</div>
<img src="" width="500" height="600" class="hello class5" id="hello5" data-src="wexqwesd">
<div class="hello class6" id="hello6" data-src="weqwvesd">Hello</div>
<img src="" width="500" height="600" class="hello class7" id="hello7" data-src="weqwzesd">
<div class="hello class8" id="hello8" data-src="wevqwesd">Hello</div>
<img src="" width="500" height="600" class="hello class9" id="hello9" data-src="weqwbesd">
<div class="hello class10" id="hello10" data-src="weqcwesd">Hello</div>
<img src="" width="500" height="600" class="hello class11" id="hello11" data-src="weqzwesd">
<div class="hello class12" id="hello12" data-src="weqswesd">Hello</div>
<img src="" width="500" height="600" class="hello class13" id="hello13" data-src="wgeqwesd">
<div class="hello class14" id="hello14" data-src="weqwhesd">Hello</div>
<img src="" width="500" height="600" class="hello class15" id="hello15" data-src="weqwvesd">
</div>
document.getElementById('wrap').querySelector('[data-src]');
document.getElementById('wrap').querySelector('div[data-src]');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
data attribute plain | |
data attribute div |
Test name | Executions per second |
---|---|
data attribute plain | 1824606.8 Ops/sec |
data attribute div | 1757576.9 Ops/sec |
Let's dive into the provided benchmark.
What is being tested?
MeasureThat.net is testing the performance of two different approaches to select elements based on their data-src
attribute:
document.getElementById('wrap').querySelector('[data-src]');
. It directly selects an element by its class name, but then filters it based on a specific attribute.document.getElementById('wrap').querySelector('div[data-src]');
. It selects elements based on their HTML data-src
attribute.Options being compared
Two options are being compared:
Pros and Cons of each approach
data-src
attribute.Library and syntax used
In the provided benchmark, the querySelector
function is used. This function is part of the Document Object Model (DOM) API in JavaScript, which provides a way to select elements based on various criteria.
No additional libraries are required for this benchmark.
Special JS feature or syntax
There is no special JavaScript feature or syntax used in this benchmark. The querySelector
function is a standard part of the DOM API and does not rely on any specific browser-specific features or syntax.
Other alternatives
If you were to implement these benchmarks from scratch, here are some alternative approaches:
getElementsByAttribute()
: Instead of using querySelector
, you could use getElementsByAttribute()
to select elements based on their data-src
attribute.data-src
attribute.[data-src]
, to select elements based on their data-src
attribute.However, these alternatives might introduce additional overhead or dependencies that are not relevant to this benchmark.