`Map.prototype.size` is O(n)
See original GitHub issueWhy is the size
accessor O(n)? It can easily be O(1) as you explicitly know when set
or delete
is called. Also please don’t make calcSize
O(n) in set/delete as you can just use a simple increment or decrement as necessary.
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Map.prototype.size - JavaScript - MDN Web Docs - Mozilla
The size accessor property returns the number of elements in a Map object.
Read more >What is the time complexity of Map.prototype.size in ...
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...
Read more >TypeError: Method get Map.prototype.size called on ...
Got this error while using @google-cloud/express-oauth2-handlers@0.1.2 which depends on sinonjs/commons@1.7.1. Doing some research about ...
Read more >24 Keyed Collections
24 Keyed Collections. 24.1 Map Objects. Maps are collections of key/value pairs where both the keys and values may be arbitrary ECMAScript language...
Read more >ES6's Map Collection Type - Haley Proctor
A Map is an object that holds key value pairs and remembers the original insertion order. This is ES6's 'improved' / alternative collection ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@Gheoan As pointed out on the mailing list,
http://www.ecma-international.org/ecma-262/7.0/index.html#sec-map-objects
The data structures used in this Map objects specification is only intended to describe the required observable semantics of Map objects. It is not intended to be a viable implementation model
The semantics surrounding size are clearly described, but we don’t need to follow the spec to the letter (per the spec itself) so long as we follow the same semantics. With that in mind, I think this is a valid issue as we can reliably make the
size
accessor much faster.Wow, TIL.
If you don’t mind leaving this open in the short term, I’ve posted a question to esdiscuss to get clarification about why the spec is the way that it is since requiring this work to get done each time on
get size
was incredibly surprising to me.Will follow up when I get a response.