4 ms·
Yesterday I had to store integers as keys in a map. It's good to learn today that they are implicitly converted to strings. Yes I could have used an array, but
by ryanlm 10y ago
Yesterday I had to store integers as keys in a map. It's good to learn today that they are implicitly converted to strings. Yes I could have used an array, but that would of required me to know the max index I would suppose. Maybe in JavaScript I insert into the array at any index, but that doesn't seem usual to me, coming from a C background I'd allocate the max index + 1.
- chrisseaton 10y agoI'm not a JavaScript expert but I believe most serious JavaScript implementations can represent sparse arrays, or arrays with holes in the range of indices, for this reason.
- BillinghamJ 10y agoVaguely relevant/example: http://imgur.com/7Bf3hLZ http://imgur.com/7Bf3hLZ
- fenomas 10y agoIf performance is important, I believe it's still a fair bit faster to use a plain object as hash, rather than a Map. (If the keys are integers V8 may initially treat the object as an array, but it'll switch over to dictionary mode pretty quickly.)
- rasz_pl 10y agojust be glad it didnt store random integers as floats (happened to me when storing integer timestamps into webstorage)