Map.prototype.size
Reports how many entries the map currently holds, as a read-only accessor property.
map.size- Added in JavaScript ES2015
Return value
numberAn integer giving the number of key/value entries currently in the Map.
Errors
Reading size on a value that is not a real Map throws a TypeError; assigning to it throws a TypeError in strict mode and is silently ignored otherwise.
Changelog
- PHP ES2015Introduced as a getter-only accessor on Map.prototype.
size is a property, not a method — write map.size, never map.size(). Calling it is the single most common Map mistake, and it fails with "map.size is not a function" rather than anything descriptive.
How it differs from Array length
Both report a count, but length on an array is writable: setting arr.length = 0 truncates it. size is defined on Map.prototype as an accessor with a getter and no setter, so map.size = 0 throws a TypeError in strict mode (which includes all module code and class bodies) and does nothing at all in sloppy mode. To empty a map use clear().
The other difference is that length can exceed the number of real elements in a sparse array. size always equals the exact number of entries, and equals [...map].length.
Testing for empty
map.size === 0 is the idiomatic check. Note that a Map object is always truthy, so if (map) is true even when the map is empty — the same trap as if ([]).
Example
When to use it
- Skip work early when a collection turned out to be empty
- Report how many unique keys a grouping produced
- Assert a cache stayed under a maximum number of entries
- Compare against an array length to count duplicates removed
Frequently asked questions
Why does map.size() throw 'is not a function'?
Because size is a property, not a method. Read it as map.size with no parentheses.
Why does a Map have size instead of length?
size is a getter-only accessor that always reflects the exact entry count, whereas an array's length is writable and can exceed the number of real elements. You cannot assign to size; use clear() to empty a map.
How do I check whether a Map is empty?
Compare map.size === 0. A Map object is always truthy, so if (map) is true even for an empty one.
More examples
size is not writable, length is
Assigning to length reshapes the array. The equivalent assignment on a Map throws, because size has no setter; clear() is the only way to reduce it to zero in one step.
const arr = [1, 2, 3];
arr.length = 1;
console.log(arr); // [1] — truncated
const m = new Map([["a", 1], ["b", 2]]);
try {
m.size = 0; // strict mode here
} catch (e) {
console.log(e.constructor.name); // TypeError
}
console.log(m.size); // still 2
m.clear();
console.log(m.size); // 0Counting duplicates removed
Later entries overwrite earlier ones with the same key, so size is the count of distinct ids. The gap against rows.length is the number of duplicates, and get(1) shows the last row won.
const rows = [
{ id: 1, name: "ann" },
{ id: 2, name: "bo" },
{ id: 1, name: "ann (dup)" },
];
const byId = new Map(rows.map((r) => [r.id, r]));
console.log(rows.length, byId.size);
console.log(rows.length - byId.size, "duplicate(s)");
console.log(byId.get(1).name);
Discussion