What is the time complexity of Map.prototype.size in JavaScript?

Viewed 290
1 Answers

Per specification, Array length is a simple read-only data property, but Map size is an accessor property that calculates the number of items by looping through the list of Map entries.

In reality, I'd expect size to be optimized by JavaScript engines so it doesn't have to loop through the list every time, but the loop is how the spec defines it.

So in theory the answer to "What is the time complexity of Map.prototype.size in JavaScript?" is O(n), but I bet the reality is much better than that.

Related