Big O Notation is the language we use for talking about how long an algorithm takes to run. It calculates how efficient is the way we solved the problem.

Check out the basics of Big O Notation here - Learn to Code - Big O Notation

To help us simplify the Big O Notation expressions we follow a set of rules.

Arithmetic Operations are constant. For example your computer takes almost same time calculating 2+2 and 20,465+2.

Variable assignments are also constant. Like incase of arithmetic operations your computer takes the same amount of time to to assign the value to a variable be it x = 3 or x= 509.

Accessing elements in an array by index, or object by key is constant.

In a loop, the complexity is the length of the loop times the complexity of whatever that happens inside the loop. For instance if we have nested loops, we are going to have

`n^2`

runtime.

Let's look at an example code -

```
function logAtLeast5(n){
for(var i=1, i<=math.max(5,n), (i++)
console.log(i);
}
}
```

Here we have a loop and this loop is going to go from one to either five or end, whichever one is larger.

What happens as 'n' grows larger so as and continues to grow and grow and grow towards infinity? The big O of this is`O(n)`

because as 'n' grows the number of operations grows in proportion with 'n'. In a graph we can see a general trajectory for O(n).

## Top comments (0)