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 |
<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")