const map = {}
for (let i=0;i<10**5;i++) {
map[i] = true
}
let ans = 0
for (let i in map) {
for (let j in map) {
ans += i+j
}
}
console.log(ans)
The above code when run using node returns the following error -
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory 1: 0x100037ddb node::Abort() [/usr/local/bin/node]
Can someone explain the reason why? The map gets instantiated just fine. Only when I loop over the map keys and add them to my ans variable I get this issue?
However the following similar code works fine and prints ans -
let ans = 0
for (let i=0;i<10**5;i++) {
for (let j=0;j<10**5;j++) {
ans += i+j
}
}
console.log(ans)
What is the logic behind this. Why is looping over keys in map so bad?
Node version v10.7.0