0

I'm trying to find out the memory usage of javascript Map objects that have ~20k entries - key is a string, and value is an array of 1-2 strings. I have created 2 Maps: with 17k and 22k entries. They both have the same memory usage in chrome profiler - how?

Why do Map objects differ in size only after Objects from which they are created get removed from the scope? Also, i know how hashmaps work, but If someone knows how the js Map can perserve order please let me know (maybe just a linked list?).

function createMap(){
    var obj = JSON.parse(bigStringRepresentingTheJSON);
    return new Map(Object.entries(obj));
}

Looking at the profiler I see that both Maps take 917kb - how can that be? But Object (obj) from which they were made takes 786kb and 1 572kb - which is reasonable.

So I thought maybe Map holds a pointer to obj from which it was created, and thats why they don't differ in size? Then, I used the createMap function so that obj gets garbage collected. Only then, Map objects take 1 9kb and 2.3kb, which is to be expected.

1 Answer 1

1

Scaling a Map is complicated, also a Hashmap is more efficient if data size << map size, as then hashes collide less often. Therefore it makes sense that a Map allocates more than needed, it probably allocates a fixed size hashtable, then scales if needed.

If thats done and how large the Map is depends entirely on the implementation though.

So I thought maybe Map holds a pointer to obj from which it was created, and thats why they don't differ in size?

No, the Map only holds references to the values in obj.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! You said "no" on my statement, then said the same thing, except "reference" instead of "poiter". Why does the Map memory usage then increase the moment when obj gets out of scope?
I do have the feeling you draw the wrong conclusions from the metrics you see. Also please read carefuly. "the values of obj" does not mean obj.
Yea, it seemed wrong to me so I asked the question. Dunno why would obj getting out of scope make profiler show map taking up more memory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.