Map.prototype.size

Counting an object means building an array of its keys first. A Map just knows, which is one of the quieter reasons to prefer it for collections that change.

Map accessorES2015Live demo
Common call
map.size
Returns
the entry count
Replaces
Object.keys(obj).length
Watch out
no parentheses, and not writable
map.size
→ number

Demo

Live evaluation
Try:
Inputs
jsonstringJSON pairs, e.g. [["a",1]]
Output
new Map(JSON.parse('[["a",1],["b",2]]')).size
2

The count is of ENTRIES, and the second case shows why that matters when building from pairs: a repeated key does not add a second entry, it overwrites the first, so three input pairs can yield fewer entries than you passed. The last case confirms keys of different types coexist happily — a string, a number and a boolean are three distinct keys. Note there are no parentheses: size is a getter.

Common patterns

Emptiness check
Reads directly, with no allocation.
if (map.size === 0) return;
Deduplicate and count
Set is the usual tool, Map when you need values too.
const unique = new Map(pairs).size;
The object equivalent
Builds an array purely to count it.
Object.keys(obj).length;

Examples

1. Two entries
new Map([["a", 1], ["b", 2]]).size
Returns
2
2. Duplicates collapse
new Map([["a", 1], ["a", 2]]).size
Returns
1
3. Empty
new Map().size
Returns
0
4. It is a getter
typeof new Map().size
Returns
'number'
5. Not writable
const m = new Map(); m.size = 5; m.size
Returns
0
6. Objects have no size
({a: 1}).size
Returns
undefined

Pitfalls

1. It is a property, not a method
map.size() throws, because the number you just read is not callable. The mirror-image mistake of array.length, and easy to make when switching between Map and the DOM APIs that do use methods.
Not callable
const m = new Map();
m.size()
TypeError: m.size is not a function
No parentheses
new Map().size
0
2. Assigning to it silently does nothing
There is no setter, so in sloppy mode the assignment is ignored and in strict mode it throws. Either way you cannot truncate a Map by setting size — unlike an array, where setting length does remove elements.
Ignored
const m = new Map([["a", 1]]);
m.size = 0;
m.size
1
Use clear
m.clear();
m.size
0
3. Duplicate pairs in the constructor collapse
Building from an array of pairs applies each in order, so a repeated key overwrites rather than adding. Code that checks the size against the input length to detect duplicates works, but only if you expected the collapse.
Fewer than passed
new Map([["a", 1], ["a", 2]]).size
1
Detect it
const pairs = [["a", 1], ["a", 2]];
pairs.length !== new Map(pairs).size
true — duplicates present
4. Objects have no equivalent
There is no size or length on a plain object, and reaching for one returns undefined rather than failing. Counting requires Object.keys, which allocates an array to throw away.
undefined
({a: 1}).length
undefined
Count the keys
Object.keys({a: 1}).length
1

When to use

Use it
  • Counting entries, or testing for emptiness
  • Anywhere Object.keys(obj).length appears in a loop
  • Detecting duplicate keys when building from pairs
Reach for something else
  • Counting values that satisfy a predicate → filter a spread
  • You want to truncate → clear, or rebuild
  • The collection is a WeakMap → it has no size, by design

Notes

Complexity
O(1) — the count is maintained as entries change
Return
A number; nothing is allocated
CPython impl
V8: the Map size accessor
Memory
No allocation
Thread-safe
Single-threaded

FAQ

length belongs to ordered, indexable things — arrays and strings. Map and Set are unordered collections in the indexing sense, so the specification uses size, as it does for the other ES2015 collections. It also keeps the two from being confused.

History

ES2015
Map, Set and their size accessors added.