question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`Map.prototype.size` is O(n)

See original GitHub issue

Why 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:closed
  • Created 6 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
dchermancommented, Mar 29, 2017

@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.

1reaction
dchermancommented, Mar 29, 2017

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found