DEV Community

Kurapati Mahesh
Kurapati Mahesh

Posted on

5 3

Big O for Javascript.

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?

So, to stress one point here: 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.


💎 Love to see your response

  1. Like - You reached here means. I think, I deserve a like.
  2. Comment - We can learn together.
  3. Share - Makes others also find this resource useful.
  4. Subscribe / Follow - to stay up to date with my daily articles.
  5. Encourage me - You can buy me a Coffee

Let's discuss further.

  1. Just DM @urstrulyvishwak
  2. Or mention
    @urstrulyvishwak

For further updates:

Follow @urstrulyvishwak

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay