jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
Measuring native Chrome promises vs generators vs callbacks
<script
src="http://jphpsf.github.io/setImmediate-shim-demo/setImmediate.js">
</script>var large = 30;
// factorial using native promises
function valuePromise(n) {
return new Promise(function(resolve) {
return resolve(n);
});
}
function factorialPromises(n) {
var start = valuePromise(n);
while (n -= 1) {
start = start.then(function(n, value) {
// console.log('n', n, 'value', value)
return value * n;
}.bind(null, n));
}
return start;
}
// factorial using generators
function * Factorial() {
var n = 1,
total = 1;
while (true) {
total = total * n++;
yield total;
};
}
function factorial(n) {
var f = Factorial(),
k, nf;
for (k = 0; k < n; k += 1) {
nf = f.next().value;
}
return nf;
}
function factorial_callback(n, callback) {
if (n <= 2)
callback(1);
else
factorial_callback(n - 1, function(res) {
callback(n * res);
});
}
function factorial_callback_setImmediate(n, callback) {
if (n <= 2)
callback(1);
else
setImmediate(function() {
factorial_callback(n - 1, function(res) {
callback(n * res);
});
});
}Ready to run.
| Test | Ops/sec | |
|---|---|---|
| native Promise | | ready |
| generators | | ready |
| callbacks | | ready |
| callbacks with setImmediate | | ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.