0

Suppose we store long strings in a Map() object:

const m = new Map()
m.set(long_string_1, 10)
m.set(long_string_2, 20)

So, the question is what exactly is being stored inside Map:

  1. original long strings
  2. their hashed digests
  3. associated values (numbers 10 an 20 in the example)

In other words does the Map stores all 1st, 2nd and 3rd, or 2nd and 3rd only? My question is related to how much memory one entry would occupy in memory in such a case.

Thanks in advance.

Hero Qu
  • 911
  • 9
  • 10
  • 1
    The keys are passed through a hash function to generate the index at which the values are stored, so the keys are not stored anywhere. Read up on a map(or hashtable) data structure implementation to learn more. Use chrome devtool to figure objects' memory footprint – Trash Can Oct 21 '18 at 07:51
  • @Dummy: Of course the keys are stored in the map. The hashcode is only an "index" to find them. – Thilo Oct 21 '18 at 07:57
  • @Dummy: thank you. But what about m.keys(), it returns the originals keys. Where are those keys stored then... – Hero Qu Oct 21 '18 at 08:01
  • I was most likely wrong about the keys not being stored anywhere since the last time I had to implement a hashtable was a long time ago, so I must have had a brain fart. But the other things I said are still correct – Trash Can Oct 21 '18 at 08:09
  • @Thilo, Dummy: Yes, thank you both. Now it's all clear: we have everything: keys, values and hashcode to find both as an entry. – Hero Qu Oct 21 '18 at 08:14

1 Answers1

-1

A Javascript Map entry comprises a key and an associated value, the specification doesn't require the key to be hashed, that's just an option.

Storage is lower-bound by key- and value-length.

The example considers strings associated with integers, "the size of a simple string with n ASCII characters is: 12 + 4 * Math.ceil(n/4)" (https://stackoverflow.com/a/68791382/19341862) whilst integers are 64 bits, which gives a lower-bound on memory consumption.

bs-
  • 1
  • 3