<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.0.min.js"></script>
//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);
var $obj = $(document.getElementById('one')); if($obj.length!=1) throw Error("Fail")
var $obj = $('#one'); if($obj.length!=1) throw Error("Fail")
var $obj = $(document.getElementById('one')).add(document.getElementById('two')); if($obj.length!=2) throw Error("Fail")
var $obj = $('#one').add('#two'); if($obj.length!=2) throw Error("Fail")
var $obj = $([document.getElementById('one'),document.getElementById('two')]); if($obj.length!=2) throw Error("Fail")
var $obj = $([$('#one')[0],$('#two')[0]]); if($obj.length!=2) throw Error("Fail")
var $obj = $('#one, #two'); if($obj.length!=2) throw Error("Fail")
var $obj = $(document.body).find('#one'); if($obj.length!=1) throw Error("Fail")
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
1 div with javascript | |
1 div with jquery selector | |
2 divs with javascript. Add with jquery | |
2 divs with 2 jquery selectors. Add with jquery | |
2 divs with javascript. Add with array | |
2 divs with 2 jquery selectors. Add with array | |
2 divs with single jquery selector | |
1 div with jquery selector with context |
Test name | Executions per second |
---|---|
1 div with javascript | 931933.2 Ops/sec |
1 div with jquery selector | 1044035.6 Ops/sec |
2 divs with javascript. Add with jquery | 244003.6 Ops/sec |
2 divs with 2 jquery selectors. Add with jquery | 247036.5 Ops/sec |
2 divs with javascript. Add with array | 692793.1 Ops/sec |
2 divs with 2 jquery selectors. Add with array | 359648.8 Ops/sec |
2 divs with single jquery selector | 171.3 Ops/sec |
1 div with jquery selector with context | 418930.5 Ops/sec |
Let's break down the provided benchmark definition and explain what's being tested, compared options, pros and cons of each approach, and other considerations.
Benchmark Definition:
The benchmark tests different ways to fetch elements into a jQuery object using JavaScript or selectors.
Test Cases:
There are 8 test cases, each testing a specific scenario:
$
function.document.getElementById
.document.getElementById
.$
function.document.getElementById
.$
function.document.getElementById('container')
) using the $
function.Comparing Options:
The benchmark compares the performance of different approaches to fetch elements into a jQuery object:
$
functiondocument.getElementById
$
functionPros and Cons:
Here's a brief summary of each approach:
.append()
, .html()
)document.getElementById
:.value
, .style
).Other Considerations:
In conclusion, this benchmark provides a comprehensive comparison of different approaches to fetch elements into a jQuery object, allowing users to evaluate their performance and choose the best approach for their specific needs.