DEV Community

Discussion on: Big O Notation as a Mid-Level Developer Who Has Been Avoiding It Since Bootcamp: Arrays and Time Complexity

Collapse
 
anthonydmays profile image
Anthony D. Mays

Well done! This is a great intro to Big O!

One thing I noticed is that you said adding to an array is constant time. Not quite correct since arrays in JavaScript really work like vectors. A true array is immutable, so if you want a bigger array than what you've got, you have to create a new, larger array and copy the old elements over to it, making the resize cost O(n). Vectors hide this from you to make it easier to work with them.

Collapse
 
clearlythuydoan profile image
Thuy Doan

Interesting! I don't know too much about vectors. If you have any intro resources, let me know. Thanks :)

Collapse
 
anthonydmays profile image
Anthony D. Mays

Sure! This article seems helpful: stackoverflow.com/questions/150790...

Note that adding to a vector is amortized constant time, meaning that it essentials averages out to constant time in normal situations. You'll also notice that retrieval from a Map or Dictionary is also linear time, but is amortized constant time.

Looking forward to your next article!