bind-verger
JavaScript performance comparison
Preparation code
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div class=el><p class=el></div>
<script>
Benchmark.prototype.setup = function() {
function isDiv(el) {
return el.tagName === 'DIV';
}
function bindVerger(method, wrapper) {
var rewrap = 0;
if (typeof wrapper === 'function') {
try {
if (wrapper('')) {
rewrap = 1;
}
}
catch (e) {
throw 'invalid wrapper';
}
}
return function(option, invert) {
var i, v, j = 0, l = this.length, fresh = rewrap ? wrapper('') : [];
invert = invert === true; // bool
for (i = 0; i < l; i++) {
v = this[i]; // in case method mutates value
// use === ! to make bool-to-bool comparison:
if (invert === !method.call(v, v, option)) {
fresh[j++] = this[i];
}
}
fresh.length = j; // in case fresh is not array
return fresh;
};
}
function bindVerger_rewrap(method, wrapper) {
var rewrap = 0;
if (typeof wrapper === 'function') {
try {
if (wrapper('')) {
rewrap = 1;
}
}
catch (e) {
throw 'invalid wrapper';
}
}
return function(option, invert) {
var i, v, l = this.length, fresh = [];
invert = invert === true; // bool
for (i = 0; i < l; i++) {
v = this[i]; // in case method mutates value
// use === ! to make bool-to-bool comparison:
if (invert === !method.call(v, v, option)) {
fresh.push(this[i]);
}
}
return rewrap ? wrapper(fresh) : fresh;
};
}
$.fn.isDiv = bindVerger(isDiv, $);
$.fn.isDiv_rewrap = bindVerger_rewrap(isDiv, $);
var $els = $('.el');
};
</script>
Preparation code output
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
fill fresh object |
|
pending… |
fill array and rewrap |
|
pending… |
$.fn.filter |
|
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 ryanve
- Revision 2: published by ryanve
- Revision 3: published
0 comments