#5523·boa

Map iterators terminate after clear() followed by insertion

Author: mansiverma897993Created Sep 14, 2026Updated Sep 14, 2026

Bug

An existing Map iterator stops after clear() followed by a new insertion, even though the newly inserted entry should still be observable by that iterator.

Reproduction

javascript
const map = new Map([[1, "first"]]);
const iterator = map.keys();

console.log(iterator.next());
// expected: { value: 1, done: false }

map.clear();
map.set(2, "second");

console.log(iterator.next());
// expected: { value: 2, done: false }

Actual behaviour

On Boa main at 257bc301, the second next() is already done ({ value: undefined, done: true }).

Expected behaviour

The second next() should return the newly inserted key (2) with done: false. Current Node 22 returns that result.

ECMAScript specifies that Map.prototype.clear() empties the existing records rather than replacing the [[MapData]] list. An iterator retains its [[MapNextIndex]], so an entry added after clear() remains observable at the end of that list. See Map.prototype.clear and %MapIteratorPrototype%.next.

Notes

This appears distinct from the closed #1092, which addressed deletion during Map.prototype.forEach. Here, OrderedMap::clear() physically clears the backing IndexMap while a MapIteratorLock is active, so the iterator's saved index points past an entry subsequently inserted into the now-empty map.

The corresponding OrderedSet::clear() follows the same physical-clear pattern, so Set iterators may need the analogous check/fix as well.

cc @jedel1043