JavaScript Map()

Coming from a (mostly) non-computer science degree background, I’m very much aware that I have some blindspots that need to be addressed.

The other things I bring to the table as a result of a decade and a half as a professional mean that I’m still able to create a lot of value for my organization, but the more I know, the better I’ll be able to contribute.

I’ve actively been working to address those blind spots, and one of the wonderful things that I’ve learned or re-learned along the way is hash tables.

Hash tables are great because a well-designed one lets you find a key and pull out the value in O(1) complexity.

JavaScript’s implementation of hash tables is the Map object. There are a lot of algorithms where a map function is hugely helpful, including the rendevous algorithm that I’ve been working on for the last week or so.

Tonight, after spending a decent amount of time debugging a number of issues with my code so far, I realized that next issue I was up against was because I thought I was saving number keys in my map rather than string keys.

map.get() and map.delete() don’t do any kind of type coercion, so you have to be very careful that you’re comparing apples to apples between the item you’re looking for and the keys in your map.

Lesson learned!

Leave a Reply