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
added check via Fermat's little theorem (seems to be slower, but doesn't cache (caching is - in this case - cheating), so the first run is as fast as the last one, no performance issues)
<script>
// var arr = [999999000001, 87178291199, 63018038201, 2147483647, 1073676287, 27644437, 1028201, 1011001, 514229, 98689, 1559, 563];
var arr = [27644437, 1028201, 1011001, 514229, 98689, 1559, 563];
// Code taken from http://ejohn.org/apps/learn/ by John Resig
function isPrime(num) {
var prime = num != 1; // Everything but 1 can be prime
for (var i = 2; i < num; i++) {
if (num % i == 0) {
prime = false;
break;
};
};
return prime;
};
function isPrimeCached(num) {
if (isPrimeCached.cache[num] !== null) {
return isPrimeCached.cache[num];
};
var prime = num != 1; // Everything but 1 can be prime
for (var i = 2; i < num; i++) {
if (num % i == 0) {
prime = false;
break;
};
};
isPrimeCached.cache[num] = prime;
return prime;
};
isPrimeCached.cache = {};
// By Devon Govett
function isPrimeFaster(num) {
var prime = num != 1 || num != 2;
for (var i = 3; i < num; i += 2) { //ignore even numbers > 2
if (num % i == 0) {
prime = false;
break;
}
}
return prime;
}
function isPrimeFasterCached(num) {
if (isPrimeFasterCached.cache[num] !== null) {
return isPrimeFasterCached.cache[num];
};
var prime = num != 1 || num != 2;
for (var i = 3; i < num; i += 2) { // ignore even numbers > 2
if (num % i == 0) {
prime = false;
break;
}
}
isPrimeFasterCached.cache[num] = prime;
return prime;
};
isPrimeFasterCached.cache = {};
// Code by Devon Govett — http://twitter.com/devongovett/status/20019256843, inspired by http://mths.be/ahf
function isPrimeRegex(n) {
return Array(n + 1).join('1').search(/^1?$|^(11+?)\1+$/) === -1;
};
function fermat(p) {
return (Math.pow(4, p) % p) === (4 % p);
}
function fermat_simplified(p) {
return (Math.pow(4, p - 1) % p) === 1;
}
function isPrime_TrialDivision(n) {
var primes = arguments.callee.primes || (arguments.callee.primes = [2, 3]);
if (n <= 1) return false;
var i, p, m, mIsPrime;
for (i = 0, p; (p = primes[i]) && (p*p <= n); i++)
if (n % p === 0) return false;
if (!p)
for (m = primes[primes.length-1] + 2; m*m <= n; m += 2) {
mIsPrime = true;
for (i = 0, p; (p = primes[i]) && (p*p <= m); i++)
if (m % p === 0) { mIsPrime = false; break; }
if (mIsPrime) {
primes[primes.length] = m;
if (n % m === 0) return false;
}
}
return true;
}
</script>
Ready to run.
Test | Ops/sec | |
---|---|---|
isPrime() |
| ready |
isPrimeCached() |
| ready |
isPrimeRegex() |
| ready |
isPrimeFaster() |
| ready |
isPrimeFasterCached() |
| ready |
Fermat's little theorem |
| ready |
Fermat's little theorem simplified |
| ready |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.