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
Test speed of Box-Muller method vs just adding three random numbers (a la central limit theorem).
Ideas from: http://www.protonfish.com/random.shtml
JSFiddle statistics: http://jsfiddle.net/wellcaffeinated/HRs7r/
<script>
function rnd_bmt() {
var x = 0, y = 0, rds, c;
// Get two random numbers from -1 to 1.
// If the radius is zero or greater than 1, throw them out and pick two new ones
// Rejection sampling throws away about 20% of the pairs.
do {
x = Math.random()*2-1;
y = Math.random()*2-1;
rds = x*x + y*y;
}
while (rds == 0 || rds > 1)
// This magic is the Box-Muller Transform
c = Math.sqrt(-2*Math.log(rds)/rds);
// It always creates a pair of numbers. I'll return them in an array.
// This function is quite efficient so don't be afraid to throw one away if you don't need both.
return [x*c, y*c];
}
function rnd_snd() {
return (Math.random()*2-1)+(Math.random()*2-1)+(Math.random()*2-1);
}
</script>
Ready to run.
Test | Ops/sec | |
---|---|---|
Box Muller |
| ready |
Central Limit method |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.