JS - dome search
JavaScript performance comparison
Preparation code
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<div class="test">
TEST
</div>
<div id="testid">
TEST ID
</div>
<div class="test">
<div id="unu">
TEST class>ID
</div>
</div>
<script>
Benchmark.prototype.setup = function() {
// Build the DOM selection query
function $q( selector, ctx ) {
ctx = ctx || document;
// Return methods for lazy evaluation of the query
return {
// Return array of all matches
all: function() {
var list, ary = [];
list = ctx.querySelectorAll( selector );
for ( var i = 0; i < list.length; i++ ) {
ary[ i ] = list[ i ];
}
return ary;
},
// Return first match
first: function() {
return ctx.querySelector( selector );
},
// Return last match
last: function() {
var list = ctx.querySelectorAll( selector );
return list.length > 0 ? list[ list.length - 1 ] : null;
}
};
}
};
</script>
Preparation code output
TEST
TEST ID
TEST class>ID
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Own JS - Class |
|
pending… |
jquery - Class |
|
pending… |
Own - ID |
|
pending… |
Jquery - ID |
|
pending… |
Own - ID |
|
pending… |
Jquery - ID |
|
pending… |
Own - all divs |
|
pending… |
Jquery - all div |
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit to the URL.
0 comments