Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

273
Views
What is the Space complexity of storing a string in JavaScript?

Are storing strings O(1) space? I am really confused about this since a mix of people saying its O(1) and O(n) at the same time.... Could someone help me on this one? I really need it for practicing for my interviews. Thank you!!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

O(1) would make sense only for complexity and not space since the size of the memory allocated for a string depends on how big it is.

As for complexity:

Depending on how the language implements it (stack or heap) it would be O(1) for stack or non-deterministic for heap, because heap allocation is handled by the OS (but as a rule of thumb, its generally O(1) if the strings are not huge).

O(n) would be for space:

When you store a string, basically what you are doing is storing an array of characters.

This would be the size of the string, since it grows linearly with the size of the string (Every character = 1 byte in a ASCII(8-bit) string).

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!