最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - JavaScript heap out of memory when looping over map? - Stack Overflow

programmeradmin2浏览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?

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?

Node version v10.7.0

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 0
Add a ment  | 

3 Answers 3

Reset to default 7

The 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)
发布评论

评论列表(0)

  1. 暂无评论