DEV Community

Gift Egbonyi
Gift Egbonyi

Posted on

Understanding Space Complexity

Hey friends! đź‘‹

Better late than never right? :)

Last week, we talked about running time which is how fast your program runs.
This week, let’s look at its quieter sibling: space complexity which is how much memory your program uses.

When you write code, it doesn’t just take time to run; it also takes memory to store variables, data structures and temporary values.
Sometimes your program runs fast but eats up too much memory. That’s where space complexity comes in.


What’s Space Complexity?

It’s the total amount of memory a program needs to complete its work.
That includes:

  • Variables and constants
  • Function call stack
  • Data structures like arrays, objects, or linked lists

You’ll often see it written like O(n), just like time complexity but here, it means memory grows with input size.


Example 1: Constant Space (O(1))

function sum(a, b) {
  return a + b; // only stores a, b, and result
}
Enter fullscreen mode Exit fullscreen mode

No matter how big your input is, this function uses the same small amount of memory.


Example 2: Linear Space (O(n))

function makeArray(n) {
  const arr = [];
  for (let i = 0; i < n; i++) {
    arr.push(i);
  }
  return arr;
}
Enter fullscreen mode Exit fullscreen mode

Here, memory grows with the array size.
If n = 10, it stores 10 numbers.
If n = 10,000, it stores 10,000 numbers. That’s O(n) space.


Why It's Relevant

Imagine running an app on a cheap phone.
If your algorithm keeps duplicating arrays or creating new objects in loops, it might crash or slow down — even if it’s fast in theory.

Understanding space complexity helps you write code that’s both efficient and scalable.


Try It Out Yourself

How It Works

  • Select an algorithm type (O(1), O(n), or O(n²)).
  • Click Run Test.
  • The chart plots how “memory usage” grows as input size increases. It’s a simulation — not real browser memory — but the pattern matches what happens in real programs.

Try Tweaking It

In the JavaScript panel, find:

const sizes = [10, 100, 500, 1000, 2000];
Enter fullscreen mode Exit fullscreen mode

Change it to:

const sizes = [100, 1000, 5000, 10000];
Enter fullscreen mode Exit fullscreen mode

Then Run Test again and watch how the curve changes.
The quadratic one will skyrocket.

👉 CodePen link: Space Complexity Visualiser


🙋🏽‍♀️ Wrap up!

Summary

Complexity Meaning Example
O(1) Constant space Simple math or fixed variables
O(n) Grows with input Building an array or list
O(n²) Grows faster Nested loops creating data

That’s it for today!
Next Wednesday, we’ll look at how time and space complexity work together and why you can’t always optimise both at once.

Till then, write clean code and stay curious!

Connect with me on GitHub

Was this tutorial helpful? Got questions? Or any insight to help me write better tutorials? Let me know in the đź’¬!


That’s it for today’s midweek mini tutorial!

I’m keeping things light, fun and useful; one small project at a time.

If you enjoyed this, leave a 💬 or 🧡 to let me know.

And if you’ve got an idea for something you'd like me to try out next Wednesday, drop it in the comments. 👇

Follow me to see more straight-forward and short tutorials like this :)

I'm still hunting for full stack roles,
contract or fulltime. 
Please check out my [Portfolio](https://gift-egbonyi.onrender.com)

:-)
Enter fullscreen mode Exit fullscreen mode

Portfolio

Web trails
You can also find me here on LinkedIn
or here X (Twitter)

✍🏾 I’m documenting my learning loudly every Wednesday. Follow along if you're learning JavaScript too!
Let’s keep learning together!

See you, same time, next Wednesday🚀

Top comments (0)