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-pacts 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
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-pacts 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?
Share Improve this question edited Nov 6, 2018 at 18:43 Yash Agarwal asked Nov 6, 2018 at 18:38 Yash AgarwalYash Agarwal 1,0451 gold badge15 silver badges28 bronze badges 0Node version v10.7.0
3 Answers
Reset to default 7The problem is your keys are strings, not numbers. You need to call parseInt() or Number() to convert them before adding:
for (let i in map) {
for (let j in map) {
ans += Number(i) + Number(j)
}
}
The loop will still take a long time (you are iterating 10**10 times), but you won't accumulated a huge string that blows up memory usage.
UPDATE: succumbed to the primacy of using Number() instead of parseInt().
when using for..in
, you are iterating over all enumerable properties, including the inherited ones on the prototype chain (so for a Object, there are quite a few)
you need to shield your loop from inherited props with hasOwnProperty
as it is outlined in the example on MDN
The reason as mentioned in accepted answer was that I was adding strings. But also converting from string to int is a costly operation specially when looping over such large amount of numbers, it will take forever.
So for someone else who is reading this question and has to use map can use Javascript Map instead of Object as used in my example above because Map can support any type of key (not just strings). So the code for that will be -
const map = new Map()
for (let i=0;i<10**5;i++) {
map.set(i, true)
}
let ans = 0
for (const i of map.keys()) {
for (const j of map.keys()) {
ans += i + j
}
}
console.log(ans)