squarerooty
JavaScript performance comparison
Preparation code
<script>
var myRoots = [];
for (var i = 0; i <= 2000; i++) {
myRoots[i] = Math.sqrt(i);
}
function mySqrt(n) {
if (n <= 0) return 0;
if (n < 1) {
var x = 1 / n;
if (x <= 2000) {
return 1 / (myRoots[x | 0]);
} else {
return Math.sqrt(n);
}
} else {
if (n <= 2000) {
return myRoots[n | 0];
} else {
return Math.sqrt(n);
}
}
}
var a = 0;
</script>
<script>
Benchmark.prototype.teardown = function() {
a = 0;
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Math with 0..1 |
|
pending… |
mySqrt with 0..1 |
|
pending… |
Math with integers |
|
pending… |
mySqrt with integers |
|
pending… |
static math |
|
pending… |
static my |
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit to the URL.
0 comments