#9 Big O in JS: The basic that you need to know

Photo by Esteban Lopez on Unsplash

There are many ways to analyse how good is a code that you have written, some people believe that a beautiful code is better and others prefer a code with fewer lines, but there a universal way to analyse that help you to understand how fast your approach is and how sustainable your system is when running millions of operations.

Its a common mistake to think that performance doesn’t matter anymore once the servers and devices processing capacity is increasing every year, we always have to remember that there are a considerable number of users using old devices or low power devices like smartphones, even updated smartphones reduce its processing capacity when the battery is running low, and yet powerful devices running low when dozen of tabs is running at the same time or several apps running together.

So, remember, performance matters, always matters.

Big O Notation analyses how fast your code will run, regardless of the processing power, so when you have to choose an approach to create your system Big O Notation will help you to select the most efficient solution, let’s take some examples:

In the example above is possible execute this operation using precisely the same time regardless the number of items inside People array, it always takes the same time, in Big O Notation its called O(1) or constant.

O(1) is the best scenario possible, always when is possible try to look for a way to use functions that have O(1) Notation, is impossible to have a better performance than this.

But in the example below the situation changes, take a look:

If you don’t know how destructuring works, take a look in this article:

Now the number of operations will increase according to the items inside an array, it is called O(n) or Linear, this approach is the second best that you could take when you’re coding something new, of course there a lot of situations that isn’t possible use functions with O(n), but sometimes we merely decide to use an approach that will spend more processing.

When you use a for each inside other for each to loop the same array you have worst performance than O(1) and O(n), like below:

This case is called O(n²) or quadratic, depending on the number of items inside your array this operation will take a very long time, and sometimes is possible to do the same using O(n), you could convert this example to this:

When you have inside an algorithm more than one O(x) you will always consider that have the worst O(x):

In this case, your algorithm has:

  1. A constant operation O(1):
const total = people.length;

2. A Linear operation O(n)

const names = people.map(({ name }) => name);

3. A Quadratic operation O(n²)

const peopleWithSameAge = people.filter(
({ age }) => people.filter(person => person.age == age).length > 1,

In this case, we could think in this way O(1) + O(n) + O(n²) right? Nope, is more simple than that, you ignore the faster operations and consider only the slower operation, in this case, O(n²), so this is an algorithm with O(n²) notation.

You’ll use this same approach when you have more than one operation with the same type:

No, this isn’t O(2n²), it’s merely O(n²), when you have constants like this you ignore the constant and use only the original notation.

Source: http://bigocheatsheet.com/

There are a lot of other examples of Big O Notation that you could discover, beyond that I showed there one that I really like called logarithmic notation or O(log(n)), in this case, the operations increase, ahn, in a logarithmic way, take a look in this famous example called binary search:

In this case the operations that you need to get the number that you’re searched increase less than in a quadratic or linear way, for example, to search for 4096 items you need only 12 operations! Cool right?

This article was an initial approach about Big O Notation. I recommend you study more about this because this will help you to code better and take the best decisions about how you write your code! I read a book called Data Structures and Algorithms in Javascript written by Loiane Groiner that helps me a lot to understand better how Big O Notation works.

Thanks for reading! Feel free to hit the recommend button below if you found this piece helpful.

If this post was helpful, please click the clap 👏 button below to show your support! ⬇⬇