jQuery trampoline
JavaScript performance comparison
Preparation code
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
Benchmark.prototype.setup = function() {
(function( $ ) {
var pluginName = 'trampoline';
var thunk = function (f, lst) {
return { tag: "thunk", func: f, args: lst };
};
var thunkValue = function (x) {
return { tag: "value", val: x };
};
var trampoline = function (thk) {
while (true) {
if (thk.tag === "value") {
return thk.val;
}
if (thk.tag === "thunk") {
thk = thk.func.apply(null, thk.args);
}
}
};
$.fn[pluginName] = function(func) {
var selector = this;
var iterator = function(n, cont) {
if (n < 1) {
return thunk(cont, [selector[n]]);
} else {
var arg = function(a) {
if (selector[a]) {
return [selector[a]];
} else {
return [selector];
}
};
var new_cont = function(v) {
var result = func.call(v, n, v);
return thunk(cont, arg(n));
};
return thunk(iterator, [n - 1, new_cont]);
}
};
return trampoline(iterator(selector.length, thunkValue));
};
})( jQuery );
};
</script>
Preparation code output
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
jQuery each |
|
pending… |
jQuery trampoline |
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit to the URL.
0 comments