JavaScript preprocessors can help make authoring JavaScript easier and more convenient. For instance, CoffeeScript can help prevent easy-to-make mistakes and offer a cleaner syntax and Babel can bring ECMAScript 6 features to browsers that only support ECMAScript 5.

Any URL's added here will be added as `<script>`

s in order, and run *before* the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.

If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.

You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.

+ add another resource

If active, Pens will autosave every 30 seconds after being saved once.

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Visit your global Editor Settings.

` ````
<script src="https://cdn.jsdelivr.net/lodash/4.10.0/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/platform/1.3.1/platform.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/2.1.0/benchmark.js"></script>
test of several primes-calculating algorythms, results in console
<div id="console"></div>
```

` ````
#console{
font-size : 35px;
}
```

` ````
var suite = new Benchmark.Suite;
function sumPrimesBasic(num) {
var res = 0;
// Function to get the primes up to max in an array
function getPrimes(max) {
var sieve = [];
var i;
var j;
var primes = [];
for (i = 2; i <= max; ++i) {
if (!sieve[i]) {
// i has not been marked -- it is prime
primes.push(i);
for (j = i << 1; j <= max; j += i) {
sieve[j] = true;
}
}
}
return primes;
}
// Add the primes
var primes = getPrimes(num);
for (var p = 0; p < primes.length; p++) {
res += primes[p];
}
return res;
}
function sumPrimesE(num) {
var max_sqrt = Math.sqrt(num); //max # needed to be checked as a divisor
//initialize array of "2" followed by all the odd #s <= num
var primes = [2];
for (var j=1; j<num/2; j++)
primes.push(j*2+1);
var i = 1; //iterate over array removing divisors of the current item
while(primes[i] <= max_sqrt) {
var pofISq = Math.pow(primes[i], 2);
primes = primes.filter((primeFilt));
i++;
}
function primeFilt(el) {
if (el < pofISq) return true; //don't need to check items less than the current item squared
return (el % primes[i] != 0); //non-divisors stay
}
return primes.reduce((a,b) => a+b);
}
function sumPrimesIntermediate(num) {
// function to check if the number presented is prime
function isPrime(number){
for (i = 2; i <= number; i++){
if(number % i === 0 && number!= i){
// return true if it is divisible by any number that is not itself.
return false;
}
}
// if it passes the for loops conditions it is a prime
return true;
}
// 1 is not a prime, so return nothing, also stops the recursive calls.
if (num === 1){
return 0;
}
// Check if your number is not prime
if(isPrime(num) === false){
// for non primes check the next number down from your maximum number, do not add anything to your answer
return sumPrimesIntermediate(num - 1);
}
// Check if your number is prime
if(isPrime(num) === true){
// for primes add that number to the next number in the sequence through a recursive call to our sumPrimes function.
return num + sumPrimesIntermediate(num - 1);
}
}
function sumPrimesOshliaer(num) {
var res = 1;
var arr = [];
var counter = 1;
while(counter <= num){
if(arr.filter(function(v){ return this % v === 0;}, counter).length < 2){
arr.push(counter);
res += counter;
}
counter += 2;
}
return res;
}
function sumPrimesP1xt(num) {
var sieve = new Array(num+1).fill(true);
var maxCheck = Math.ceil(Math.sqrt(num));
var i, j;
var results = 0;
for (i = 2; i <= maxCheck; i++) {
if (sieve[i] === true) {
for (j = i * i; j <= num; j += i) {
sieve[j] = false;
}
}
}
for (i=2; i<=num; i++) {
if (sieve[i]) results += i;
}
return results;
}
// add tests
var runs = 10;
suite
.add('sumPrimesBasic', function() {
sumPrimesBasic(runs);
})
.add('sumPrimesIntermediate', function() {
sumPrimesIntermediate(runs);
})
.add('sumPrimesOshliaer', function() {
sumPrimesOshliaer(runs);
})
.add('sumPrimesP1xt', function() {
sumPrimesP1xt(runs);
})
.add('sumPrimesE', function() {
sumPrimesE(runs);
})
// add listeners
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
// run async
.run({ 'async': true });
```

999px

Also see: Tab Triggers