Big

O

Big O

Big O is an algorithm to determine the complexity/efficiency of your code.

Can be calculated as O(n) with n representing the variable of time.

Big O

O(1)

This formula means that the complexity of the code does not change no matter the size of the input

function findFirst(input){
    return input[0]
}

Big O

O(n)

This formula means that the complexity of the code changes linearly in regards to input size (because we have to loop)

function findThree(input, value){
    for(let i = 0; i < input.length; i++){
        if(input[i] === value){
            return `found ${value}!`
        }
    }
    return `value not found`
}

findThree([3,1,2], 3) // 1 iteration (as good as it gets)
findThree([0,1,2,4], 3) // 4 iterations (not so good but four loops isn't too bad)
findThree([0,1,2,REALY LONG ARRAY, 3], 3) // who knows. 3 is at the end so.. (as bad as it gets)

Big O

O(N2)

This formula means that the complexity of the code changes based on the squared input (because we have nested loops)

function makeTuples(input) {
    let answer = [];
    for (let i = 0; i < input.length; i++) {
        for (let j = 0; j < input.length; j++) {
            answer.push([input[i], input[j]]);
        }
    }
    return answer;
}

Big O

O(log n)

This formula is more complex and gets pretty deep into some math.

Imagine a scenario where you needed to find a name in a phone book. You half the phone book and look for it there. If you find it, return the name, if not, half the phone book again with your just searched half. Continue this process until you find the name.

Big O

Essentially you can count the number of loops and that will be your value for n. The more loops, the more complex it will be to perform with regards to the size of your input.

Copy of Big O

By jonmcd

Copy of Big O

  • 173