Big O is one of the most important topics for any software developer or engineer. No matter what company you are working, where you live, or how long it will be from now, this concept will be around and it is what makes you a better developer. Big companies, like F.A.A.N.G., all know this which is why you won't get any of their interviews without encountering this topic.
1. What is Big O
Big O, the official term is Big O asymptomatic analysis, can help us to see how well a problem is solved. Any coder, who is given enough time, can solve a problem. However, what matters is how well the problem is solved. We will talk about what Big O is, how we define it and then how to use Big O and it's different notation to distinguish bad code from good code, or good code from better code.
2. What is Good Code?
Good code is defined by two criteria: Readability and Scalability. Readability determines if your code is just generally clean, and if others can understand your code. Scalability is the ability to adapt of your code. Big O is what allows us to measure this idea of scalable code.
3. Big O and Scalability
Consider a cake baking procedure as an example of scalability. Let's say, we have a task to bake a cake with provided recipe and a kitchen. There are many way to bake a cake which will result in either a good or a bad product. It all depends on how well we utilize the kitchen to work well with the provided recipe. We can say, our baking procedure must adapt to the provided resources that we have, in other words, it must scalable with the available resources.
Well, computers are machine which need to work in order to produce something for human. Computers work the same way as the kitchen in the example above. We have instructions that we give it through code and these code are running by computers to produce some sort of an output. A coder is someone who gives the code as instruction to a computer, just like how to use recipe to bake a cake, there are many ways to code and solve problem which will result in either good or bad outcome. So, your code performance must scalable to the provided machine which executes it. Big O notation is used to measure how well your code performs in any computer.
So, how is Big O used to measure your code performance? Consider below piece of JavaScript code. We are writing a simple function to catch Pikachu!
const pokemon = ['Pikachu', 'Meowth', 'Bulbasaur']; function catchPikachu (array) { for (let i = 0; i < array.length; i++) { if (array[i] === 'Pikachu') { console.log('Catch Pikachu!'); } } } catchPikachu(pokemon);
The above code would run really fast in modern computers because, well, computers are fast now! The other factor that make it really fast is the list of input array is small. We only have 3 items in our pokemon array. Let's try it again with another array of more Pokemon and have a little trick to calculate how long your code take to execute in my machine.
const pokemon = ['Pikachu', 'Meowth', 'Bulbasaur']; const morePokemon = ['Pikachu', 'Meowth', 'Bulbasaur', 'Charmander', 'Squirtle', 'Caterpie', 'Weedle', 'Pidgey', 'Rattata', 'Spearow']; const manyPokemon = new Array(1000000).fill('Pikachu'); function catchPikachu (array) { let t0 = performance.now(); for (let i = 0; i < array.length; i++) { if (array[i] === 'Pikachu') { console.log('Catch Pikachu!'); } } let t1 = performance.now(); console.log('Call to catch Pikachu took ' + (t1 - t0) + ' milliseconds'); } catchPikachu(pokemon); //took roughly 0 millisecond catchPikachu(morePokemon); //took roughly 0.1 millisecond catchPikachu(manyPokemon); //took roughly 7 millisecond
Well, we can see different outcomes depend on the size of the input, and, of course, the machine we use to execute the code. One of the reason you may have different results of how long your code took than mine is because you ran your code in your computer and I ran mine in my computer.
Big O notation is the language which is used for talking about how long an algorithm takes to run. We can compare two different algorithms using Big 0 to say which one is better than the other in term of scale regardless of our computer and input differences.
So, we have been introduced to Big O and have an idea of the use of it. We will discuss how Big O is calculated and which Big O number represent horrible and excellent code performance in the next posts.