simple jquery selector vs 140medley
JavaScript performance comparison
Preparation code
<div>
<ul id="list">
<li class="first">
</li>
<li>
</li>
</ul>
</div>
<div class="first">
</div>
<script>
var re = new RegExp("^(\W)?(.*)");
var $$ = function(
a, // take a simple selector like "name", "#name", or ".name", and
b // an optional context, and
){
a = a.match(re); // split the selector into name and symbol.
return( // return an element or list, from within the scope of
b // the passed context
|| document // or document,
)[
"getElement" + ( // obtained by the appropriate method calculated by
a[1]
? a[1] == "#"
? "ById" // the node by ID,
: "sByClassName" // the nodes by class name, or
: "sByTagName" // the nodes by tag name,
)
](
a[2] // called with the name.
)
}
$id = function(id) {
return document.getElementById(id);
}
$tag = function(tag) {
return document.getElementsByTag(tag);
}
$class = function(name) {
return document.getElementsByClassName(name);
}
$extra= function(selector, context)
{
var a = context || document;
switch(selector[0]){
case '#': return a.getElementById(selector.slice(1));
case '.': return a.getElementsByClassName(selector.slice(1));
}
return a.getElementsByTagName(selector);
}
</script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
Preparation code output
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
find by Id (medley) |
|
pending… |
find by Id(jquery) |
|
pending… |
find by id (vanilla) |
|
pending… |
find by id (wrapper) |
|
pending… |
find by id ($extra) |
|
pending… |
find elements ($extra) |
|
pending… |
find elements (jquery) |
|
pending… |
find elements (medley) |
|
pending… |
Compare results of other browsers
Revisions
You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:
- Revision 1: published by Manuel Astudillo
- Revision 2: published
- Revision 3: published
- Revision 4: published
0 comments