//Add 100,000 elements
var frag = document.createDocumentFragment();
for (var i=0; i<10; i++){
var outDiv = document.createElement('div');
for (var j=0; j<100; j++){
var midDiv = document.createElement('div');
for (var k=0; k<100; k++){
var inDiv = document.createElement('div');
if(i==6 && j==60){
if(k==60)
inDiv.id="one";
else if(k==61)
inDiv.id="two";
}
midDiv.appendChild(inDiv)
}
outDiv.appendChild(midDiv)
}
frag.appendChild(outDiv);
}
document.body.appendChild(frag);
// A simple way to check for HTML strings
// Prioritize #id over <tag> to avoid XSS via location.hash (#9521)
// Strict HTML recognition (#11290: must start with <)
// Shortcut simple #id case for speed
var rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]+))$/;
var selectorOne = '#one';
var selectorTwo = '#two';
var selectorBoth = selectorOne + ',' +selectorTwo;
var elements = document.querySelectorAll(selectorBoth);
if( $(elements).length != 2 ) throw Error()
var selectors = selectorBoth.split(',');
var ids = true;
for(var i=0; i<selectors.length; i++)
{
var match = rquickExpr.exec( selectors[i] );
ids = ids && (selectors[i] = match[2]);
}
var elements = [];
if(ids){
for(selector in selectors){
elements.push( document.getElementById(selector) )
}
}
else
{
elements = document.querySelectorAll(selectorBoth)
throw Error("querySelectorAll");
}
if( $(elements).length != 2 ) throw Error()
if( $('#one').add('#two').length != 2 ) throw Error()
if( $([document.getElementById('one'),document.getElementById('two')]).length != 2 ) throw Error()
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Original selector | |
Parse selector | |
select one two jquery add | |
Select one two array |
Test name | Executions per second |
---|---|
Original selector | 171.6 Ops/sec |
Parse selector | 93266.0 Ops/sec |
select one two jquery add | 80721.3 Ops/sec |
Select one two array | 250662.7 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Definition
The benchmark is testing three different approaches to select two ID'd elements from the DOM:
document.getElementById()
method to select each element individually (e.g., document.getElementById('one')
).$()
function with the .add()
method to combine two selectors.Options being compared
The benchmark is comparing the performance of these three approaches:
document.getElementById()
for each element.$()
function with the .add()
method.Pros and cons of each approach
.add()
method.Library and its purpose
The benchmark uses jQuery's $()
function with the .add()
method, which is a convenience wrapper around document.getElementById()
. This allows for a simple way to chain multiple selections together. However, it's essential to note that this implementation relies on jQuery, which may not be desirable in all scenarios.
Special JS feature or syntax
There are no special JavaScript features or syntax mentioned in the benchmark definition. The focus is on comparing different selection methods rather than exploring advanced JavaScript concepts.
Other alternatives
Besides the three approaches being compared, there are other methods to select elements from the DOM:
#one #two
) can also be used to select elements.document.getElementById()
or querySelector
.It's worth noting that the benchmark result shows that the "Parse selector" approach performs better than the individual selectors, while the jQuery-style selector approach is slower. However, these results may vary depending on the specific use case, browser version, and platform being tested.