JavaScript JavaScript heap out of memory when looping over map? javascript heap memory error,javascript heap memory increase,javascript heap

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

Answer:1

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().

Answer:2

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

Answer:3

I'm using RSpec + capybara, and the capybara-webkit as driver. I have to check if a JS box exists in the page after clicking on a button, but with no results. If I use selenium as a driver, the test ...

I'm using RSpec + capybara, and the capybara-webkit as driver. I have to check if a JS box exists in the page after clicking on a button, but with no results. If I use selenium as a driver, the test ...

  1. check exists in sql
  2. check file exists with javascript
  3. check if file exists with python
  4. check if file exists with wildcard bash
  5. check if file exists with powershell
  6. check if element exists with jquery
  7. check if file exists with curl
  8. check if file exists with javascript
  9. check if file exists with bash
  10. check if service exists with ansible
  11. check if file exists with ifstream
  12. check if class exists with jquery
  13. check if file exists with vba
  14. check if file exists with wget
  15. check if file exists with php
  16. check if row exists with mysql
  17. check if user exists with ansible
  18. check if directory exists with powershell
  19. check if folder exists with powershell

I have a div in which I have a button and a textarea. What is the easiest way to get my hands on a child from a given div by its id? <html> <head> <link rel="stylesheet" ...

I have a div in which I have a button and a textarea. What is the easiest way to get my hands on a child from a given div by its id? <html> <head> <link rel="stylesheet" ...

  1. element same line css
  2. element same id html
  3. element same number of protons and neutrons
  4. element same
  5. element same atom
  6. element same as molecule

I'm using Template.uploadFile.events({ 'change .set-file': function ( event, template ) { var file = event.currentTarget.files[0]; [...] } }); when uploading a file. I want to read ...

I'm using Template.uploadFile.events({ 'change .set-file': function ( event, template ) { var file = event.currentTarget.files[0]; [...] } }); when uploading a file. I want to read ...

  1. shiny update reactive variable
  2. r shiny update reactive variable
  3. shiny update reactive values

I have a screen in my app that has a "in between" content. This content is too long for the Iphone 5-8, but just one screen for the Iphone 8S-XS Max. To fix it, I've put on a simple ScrollView, ...

I have a screen in my app that has a "in between" content. This content is too long for the Iphone 5-8, but just one screen for the Iphone 8S-XS Max. To fix it, I've put on a simple ScrollView, ...

  1. react native scrollview fill height
  2. react native scrollview full screen