How JavaScript Maps Can Make Your Code Faster

Why you might be better off without a regular JavaScript object

Bret Cameron
5 min readAug 5, 2019

Among the goodies introduced to JavaScript in ES6, we saw the introduction of Sets and Maps. Unlike ordinary objects and arrays, these are ‘keyed collections’. That means their behaviour is subtly different and — used in the right contexts — they offer considerable performance advantages.

In a previous article, I looked into Sets and how they can help us write faster, cleaner JavaScript. In this article, I want to do the same for Maps. I’ll discuss how they are different, where they come in handy, and where they can offer performance benefits over regular JavaScript objects.

How are Maps different to Objects?

There are two main differences between Maps and regular JavaScript objects.

1. Unrestricted Keys

Each key in an ordinary JavaScript object must be either a String or a Symbol . The object below demonstrates this:

const symbol = Symbol();
const string2 = 'string2';
const regularObject = {
string1: 'value1',
[string2]: 'value2',
[symbol]: 'value3'
}

--

--

Bret Cameron
Bret Cameron

Written by Bret Cameron

Writer and developer based in London. On Medium, I mainly write about JavaScript, web development and Rust 💻

Responses (20)