Better Programming

Advice for programmers.

Follow publication

Member-only story

Stop Using Objects as Hash Maps in JavaScript

Johannes Baum
Better Programming
Published in
2 min readApr 14, 2020
Photo by Steve Johnson on Unsplash.

A map is one of the most frequently used data structures in daily programming. It keeps key-value pairs that can easily be accessed by their keys. In Java, it is pretty obvious to use a HashMap for that. However, in JavaScript, it is quite convenient to just use a plain object for that purpose:

But there is a built-in data structure in JavaScript that was made for exactly that purpose: Map. Let me give you some reasons to prefer Map over plain objects.

1. More Key Types

Objects can only have symbols or strings. Maps can have any type of value as a key: objects, functions, or primitives.

2. Better Size Determination

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Johannes Baum
Johannes Baum

Written by Johannes Baum

Creator of GridEngine (https://github.com/Annoraaq/grid-engine) 👾 Software Engineer 🚀 JavaScript/TypeScript

Write a response