Big O for Javascript.
Tags: javascript, discuss, web3, webdev
Published: 2022-04-02 08:16:55

Yes. Big O plays a crucial role in every programming language. Basically, tells how efficiently did you write the code. Technically, speaks about performance of the code. 

_Which has two items to consider:_

_Time Complexity_ - Speaks about how fast your program runs
_Space complexity_ - How much memory gets consumed?

<u>So, to stress one point here:</u> Big O is more like high level or generic way of calculating performance. To simply put, it's not the exact time or space that your program takes but talks about worst case performance. Hence, the best metric than any other methods.

Time complexity refers to the count of operations being performed in your code. 

Space complexity refers to the count of variables or new objects are created.

Basically, we have 3 types:

O(1) - Constant
O(n) - linear
O(n^2) - quadratic

There are two more types which includes `log` are:

O(log n)
O(nlogn)

Below is the order in which your code will become better.

O(n^2) -> O(nlogn) -> O(n) -> O(logn) -> O(1)
Worst ---------------------------------> Best

Let's see few simple built in JS methods and their complexities:

JS Object:
**Operation: Time Complexity**

1. Insertion: O(1)
2. Removal: O(1)
3. Access: O(1)
4. Search: O(n)

JS Object Methods:

1. Object.Keys(): O(n)
2. Object.values(): O(n)
3. Object.entries(): O(n)
4. Object.hasOwnProperty(): O(1)

JS Arrays: 
1. Searching: O(n)
2. Access: O(1)
3. push(), pop(): O(1)
4. shift(), unshift(): O(n)
5. concat(), slice(), splice(): O(n)
6. sort: O(nlogn)
7. forEach/map/filter/reduce etc.: O(n)

Generally, space complexity has no big significance in javascript.

Let me know if you want to understand how these complexities are calculated or you want Big O for any challenging tasks. I am always happy to help. 

Thank you. 

<hr style="border-width: 2px;border-color: #87CEEB;">
<h1> 💎 Love to see your response</h1>
<ol>
<li style="padding: 10px;font-size: 30px;"><b>Like</b> - You reached here means. I think, I deserve a like.  </li>
<li style="padding: 10px;font-size: 30px;"><b>Comment</b> - We can learn together. </li>
<li style="padding: 10px;font-size: 30px;"><b>Share</b> - Makes others also find this resource useful.</li>
<li style="padding: 10px;font-size: 30px;"><b>Subscribe / Follow</b> - to stay up to date with my daily articles. </li>
<li style="padding: 10px;font-size: 30px;"><b>Encourage me</b> - <a href="https://www.buymeacoffee.com/urstrulyvishwak" target="_blank">You can buy me a Coffee</a>
</ol>
<hr style="border-width: 2px;border-color: #87CEEB;">

<h2> Let's discuss further. </h2>

<ol style="padding: 20px;">
<li style="padding: 10px;font-size: 30px;">Just DM <br> <a href="https://twitter.com/messages/compose?recipient_id=1388837509824610314&text=Hello world"
class="twitter-dm-button" data-screen-name=“@urstrulyvishwak” data-size="large">@urstrulyvishwak</a>
</li>
<li style="padding: 10px;font-size: 30px;">
<p> Or mention <br> <a href="https://twitter.com/intent/tweet?screen_name=urstrulyvishwak&ref_src=twsrc^tfw" class="twitter-mention-button" data-size="large" data-text="Hi, Vishwak" data-related="@urstrulyvishwak" data-show-count="false">@urstrulyvishwak</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script></p> </li>
</ol>

<h2> For further updates: </h2>
<a href="https://twitter.com/urstrulyvishwak?ref_src=twsrc^tfw" class="twitter-follow-button" data-size="large" data-text="Hi, Vishwak" data-related="@urstrulyvishwak" data-show-count="false">Follow @urstrulyvishwak</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>