Big O Notation

Joseph K Abe
2 min readDec 11, 2021

Something that I have seen frequently, but glossed over is Big O notation. I finally decided to look deeper into it and regret not doing it sooner. What Big O Notation is, is a way to gage the efficiency of an algorithm. As you know some code takes longer than others to compute. Big O makes it a bit easier to judge how much time and space your code will take up.

There are 3 main types of code (in respect to efficiency) the first being constant, the second linear, the third quadratic.

The first (and most efficient) that I am going to cover is constant notation. This means that the code you write will have a constant run time. Something like…

const addNums = (num1, num2) => num1 + num2

is a constant or O(1). O(1) is the shorthand way of saying the function has a constant O notation. The function is constant because the number of actions the computer must take to complete the function is basically constant. The computer will add what ever two numbers that we input.

A function with a linear notion will grow incrementally with the input. Methods like forEach() and map() have a linear notation. For example..

const addOne = (nums) {
for (let i = 0; i < nums.legnth; i++){
return nums[1] + 1
}
}

is a linear or O(n) notion. In this case every number in the nums array will be added 1 which means unlike our last example the amount of times the computer will have to work will depend on the array’s length. Obiviouolsy this is slower than a function with constant notation.

The last notion I am going to cover in this installment is quadratic notation. This is by far the slowest. An example of a function with a quadratic runtime is a nested loop.

cost addNestedOne = (arrayOfNums) => 
const numArray = arrayOfNums[i]
const num = numArray[i]
for (let i = 0; i < nums.length; i++) {
for (let i = 0; i < numArray.length; i++){
return num + 1
}
}
}

As you can see in this example the function’s runtime depends on both the number of arrays and the numbers in each array giving it a quadratic notion, or an O(n ^2) notion. Obviously this will take a great deal of computing to run.

--

--