Big O Notation and Algorithm Analysis with JavaScript Examples

Big-O Cheat Sheet

Objectives

  • Definition of Big O Notation
  • Explain Simple Big O Expressions
  • Explain Time Complexity
function addUpTo(n) {
let total = 0;
for (let i = 1; i <= n; i++) {
total += i
}
return total;
}console.log(addUpTo(3))

2- Solution

function addUpTo(n){
return n * (n + 1) / 2;
}
console.log(addUpTo(3))
function addUpTo(n) {
let total = 0;
for (let i = 1; i <= n; i++) {
total += i
}
return total;
}let t1 = performance.now();
addUpTo(1000000000)
let t2 = performance.now();
console.log(`Time Elapsed: ${(t2 - t1)/100} seconds.`)
console(output)
function addUpTo(n){
return n * (n + 1) / 2;
}
console.log(addUpTo(3))let t1 = performance.now();
addUpTo(1000000000)
let t2 = performance.now();
console.log(`Time Elapsed: ${(t2 - t1)/100} seconds.`)
console(output)