A brief overview of Big O Notation with JavaScript

Big O Notation

O(1)

function returnSquare(n) {
return n*n
}

O(n)

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

O(n²)

function nestedLoop(n) {    for(let i = 0; i < n; i++) {        for(let j = 0; j < n; j++) {            console.log(i,j)        }    }}

O(log n)

function binarySearch(arr, val) {    let left = 0    let right = arr.length-1    while(left <= right) {        let pointer = Math.floor((right+left)/2);        if(arr[pointer] === val) {            return pointer        }        if(arr[pointer] < val) {            left = pointer + 1        }        if(arr[pointer] > val) {            right = pointer - 1        }    }    return -1}

O(n log n)

From https://www.bigocheatsheet.com/

Software Engineering Student

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store