Created
February 13, 2014 08:53
-
-
Save fidelisrafael/8971907 to your computer and use it in GitHub Desktop.
Javascript is not a yot (http://techteam.wordpress.com/2012/07/18/javascript-is-not-a-toy/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var runOptimized = function() { | |
"use strict"; | |
function getPrimes(){ | |
var primeCount, primes, obj; | |
primeCount = 0; | |
primes = new Array(100000); | |
obj = {}; | |
obj.getPrimeCount = function() { return primeCount; }; | |
obj.getPrime = function(i) { return primes[i]; }; | |
obj.addPrime = function(p) { primes[primeCount]=p; primeCount+=1; }; | |
obj.isPrimeDivisible = function(candidate){ | |
var i, currentPrime; | |
for(i=1; i<primeCount; i+=1){ | |
currentPrime = primes[i]; | |
if(currentPrime*currentPrime>candidate) { return false; } | |
if((candidate % currentPrime)===0) { return true; } | |
} | |
return false; | |
}; | |
return obj; | |
} | |
function main(){ | |
var p, c; | |
console.log("JS 25,000th prime"); | |
console.time("primes_count_optimized") | |
p = getPrimes(); | |
c = 1; | |
while(p.getPrimeCount()<25000){ | |
if(!p.isPrimeDivisible(c)){ | |
p.addPrime(c); | |
//console.log("added prime ", c); | |
} | |
c++; | |
} | |
console.log(p.getPrime(p.getPrimeCount()-1)); | |
console.timeEnd("primes_count_optimized") | |
} | |
main(); | |
} | |
var runNormal = function() { | |
"use strict"; | |
function getPrimes(){ | |
var primeCount, primes, obj; | |
primeCount = 0; | |
primes = new Array(100000); | |
obj = {}; | |
obj.getPrimeCount = function() { return primeCount; }; | |
obj.getPrime = function(i) { return primes[i]; }; | |
obj.addPrime = function(p) { primes[primeCount]=p; primeCount+=1; }; | |
obj.isPrimeDivisible = function(candidate){ | |
var i, currentPrime; | |
for(i=1; i<primeCount; i+=1){ | |
currentPrime = primes[i]; | |
if((candidate % currentPrime)===0) { return true; } | |
} | |
return false; | |
}; | |
return obj; | |
} | |
function main(){ | |
var p, c; | |
console.time("primes_count") | |
console.log("JS 25,000th prime"); | |
p = getPrimes(); | |
c = 1; | |
while(p.getPrimeCount()<25000){ | |
if(!p.isPrimeDivisible(c)){ | |
p.addPrime(c); | |
//console.log("added prime ", c); | |
} | |
c++; | |
} | |
console.log(p.getPrime(p.getPrimeCount()-1)); | |
console.timeEnd("primes_count") | |
} | |
main(); | |
} | |
console.log("running normal JS Code"); | |
runNormal(); | |
console.log("running optimized JS Code"); | |
runOptimized(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
function getPrimes(){ | |
var primeCount, primes, obj; | |
primeCount = 0; | |
primes = new Array(100000); | |
obj = {}; | |
obj.getPrimeCount = function() { return primeCount; }; | |
obj.getPrime = function(i) { return primes[i]; }; | |
obj.addPrime = function(p) { primes[primeCount]=p; primeCount+=1; }; | |
obj.isPrimeDivisible = function(candidate){ | |
var i, currentPrime; | |
for(i=1; i<primeCount; i+=1){ | |
currentPrime = primes[i]; | |
if((candidate % currentPrime)===0) { return true; } | |
} | |
return false; | |
}; | |
return obj; | |
} | |
function main(){ | |
var p, c; | |
console.time("primes_count") | |
console.log("JS 25,000th prime"); | |
p = getPrimes(); | |
c = 1; | |
while(p.getPrimeCount()<25000){ | |
if(!p.isPrimeDivisible(c)){ | |
p.addPrime(c); | |
//console.log("added prime ", c); | |
} | |
c++; | |
} | |
console.log(p.getPrime(p.getPrimeCount()-1)); | |
console.timeEnd("primes_count") | |
} | |
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
function getPrimes(){ | |
var primeCount, primes, obj; | |
primeCount = 0; | |
primes = new Array(100000); | |
obj = {}; | |
obj.getPrimeCount = function() { return primeCount; }; | |
obj.getPrime = function(i) { return primes[i]; }; | |
obj.addPrime = function(p) { primes[primeCount]=p; primeCount+=1; }; | |
obj.isPrimeDivisible = function(candidate){ | |
var i, currentPrime; | |
for(i=1; i<primeCount; i+=1){ | |
currentPrime = primes[i]; | |
if(currentPrime*currentPrime>candidate) { return false; } | |
if((candidate % currentPrime)===0) { return true; } | |
} | |
return false; | |
}; | |
return obj; | |
} | |
function main(){ | |
var p, c; | |
console.log("JS 25,000th prime"); | |
console.time("primes_count_optimized") | |
p = getPrimes(); | |
c = 1; | |
while(p.getPrimeCount()<25000){ | |
if(!p.isPrimeDivisible(c)){ | |
p.addPrime(c); | |
//console.log("added prime ", c); | |
} | |
c++; | |
} | |
console.log(p.getPrime(p.getPrimeCount()-1)); | |
console.timeEnd("primes_count_optimized") | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To test using node:
curl -s https://gist.github.com/fidelisrafael/8971907/raw/7eb1f390687525073c8ad61ad1c99b4d501bc7b4/bench.js > js-bench.js && node js-bench.js