big O TLDR;
Big O is a notation used to describe the computational complexity of an algorithm. The computational algorithm is split into two parts:
- Time complexity or the amount of time required to run the algorithm relative to the input size
- Space complexity of how much memory is allocated by the algorithm relative to the input size
constant time
The best complexity is called constant time
or constant space
, meaning that the algorithm always uses the same amount of resources regardless of the input.
Time complexity
complexity
for (const n of arr) {
console.log(n);
}
This algorithm has a time complexity of . Each loop of the iteration costs .
complexity
for (const n of arr) {
for (const n2 of arr) {
console.log(n & n2);
}
}
This algorithm has a time complexity of :
- outer loop costs
- inner loop costs
complexity
for (const n of arr) {
console.log(n);
}
for (const n of arr) {
console.log(n);
}
for (const m of arr2) {
console.log(m);
}
- first loop costs
- second loop costs
- the third loop costs
- which in big O notation is because the constant is ignored.