Detect prime numbers (v11)

Preparation HTML

``````<script>
var arr = [ 10, 137165461];

// 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 ferm(p) {
if (p - 3 < 0) return true;
if (p%2 === 0) return false;
return (Math.pow(4, p - 1) % p) === 1;
}
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 = {};

// 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;
};
</script>``````

Test runner

Testing in
TestOps/sec
isPrime()
``````var i = arr.length;
while (i--) {
isPrime(arr[i]);
};``````
isPrimeCached()
``````var i = arr.length;
while (i--) {
isPrimeCached(arr[i]);
};``````
isPrimeRegex()
``````var i = arr.length;
while (i--) {
isPrimeRegex(arr[i]);
};``````
ferm()
``````var i = arr.length;
while (i--) {
ferm(arr[i]);
};``````