DEV Community

Cover image for Easy as a pie Big O notation. Part 2: Space.
Elizabeth Villalejos
Elizabeth Villalejos

Posted on • Edited on

2

Easy as a pie Big O notation. Part 2: Space.

What?

When talking about space complexity, it's common that we hear the term 'Auxiliary space complexity'. This refers to the space required by the algorithm (not the space taken by inputs).

Why?

While it might be more pressing at some cases to look at time complexity, there will be times when you need to consider the environment in which your code will be executed and whether you will have the resources to do so.

AHA! moment

Important tips to look out for

  • Primitives are constant space (booleans, numbers, undefined, null)
  • Strings require O(n) space (where n is the string length)
  • Reference types are usually O(n) too, where n is either the length for arrays or number of keys for objects

In space complexity, we also use logarithms to express performance. So whenever you see O(log n) it's safe to say we're talking about space, not time complexity.

About logarithms

A logarithm is the inverse of exponentiation. The logarithm of a number roughly measures the number of times you can divide that number by 2 before you get a value that’s less than or equal to one.

Where can we expect to see logarithms?

Certain searching algorithms have logarithmic time complexity.
Efficient sorting algorithms involve logarithms.
Recursion sometimes might involve logarithmic space complexity.

So, where does this leave us?

To sum it up,
-Big O notation is used to analyze algorithm performance.
-It can give us a high level of understanding of the time or space complexity of it.
-It cares about general trends, not details.
-Time or space complexity depends only on the algorithm, not the hardware where it’s running.

get-it

Got something to add? Please feel free to reach out for any question, comment or meme.

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

Top comments (0)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video

👋 Kindness is contagious

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

Okay