function sum1 (n) {
  let sum = 0;
  for(let i=1;i<=n;i++){
    sum+=i;
  }
  return sum;
}

function sum2 (n) {
  return (1 + n) * n/2;
}

const timer1 = window.performance.now();
console.log(sum1(1000000));
const timer2 = window.performance.now();
console.log(`sum1 used ${(timer2 - timer1)/1000} to execute`);
const timer3 = window.performance.now();
console.log(sum2(1000000));
const timer4 = window.performance.now();
console.log(`sum2 used ${(timer4 - timer3)/1000} to execute`);

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.