As covered briefly by MDN, Map has a number of technical benefits — while a simple Array or an Object may suffice for simple data structures, oftentimes the increasing complexity of modern web development justifies using something else.

Let's look at a few specific examples:

The keys of a Map can be anything

In both modern libraries as well as webapps, memoization is often used to cut down performance costs of repetitive operations. Briefly, memoization is the act of storing the output of a given function for given inputs after the first run.
Maps are a great candidate for storing this data, since you can use them regardless of what you want to index by.

Show code snippet

const cache = new Map();
const input = [2, 3, 2]; // Assuming this is immutable for simplicity

function expensive(n, ...args) {
  if (!args.length) return n;
  return Math.pow(n, expensive(...args));
}

function memoExpensive(arr) {
  const memoResult = cache.get(arr);
  if (memoResult) {
    console.log('used memo', memoResult);
    return memoResult;
  }
  const result = expensive(...arr);
  console.log('calculated', result);
  cache.set(arr, result);
  return result;
}

memoExpensive(input);
memoExpensive(input);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

There are cases where you could also use a Symbol and a regular Object, but a Map is more universal in this regard.

Map keys are ordered

For a regular Object, keys are partially ordered (since ECMAScript 2015, before that it was a bit iffy on different engines). Keys keep their order only for string and Symbol keys, while numerical keys are sorted.
When adding keys to an Object in the order c, b, a, 3, 2, 1, the stored order will be 1, 2, 3, c, b, a, for example.

Show code snippet

const obj = {};
obj.c = 1;
obj.b = 2;
obj.a = 3;
obj[3] = 4;
obj[2] = 5;
obj[1] = 6;
console.log(Object.keys(obj));
Run code snippetEdit code snippet Hide Results Copy to answer Expand

A Map will keep the order of the inserted keys regardless of their type.

Show code snippet

const map = new Map();
map.set('c', 1);
map.set('b', 2);
map.set('a', 3);
map.set(3, 4);
map.set(2, 5);
map.set(1, 6);
console.log(Array.from(map.keys()));
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Oftentimes this won't matter, especially in casual web dev, but there are numerous notorious cases when the order of the keys does matter.

Map is iterable

While an Array, as brought up in the question, is also iterable, a Map offers a better use-case for key-value mappings than an Object would. Being iterable has a number of benefits, just-in-time execution and guaranteed order being two examples.

Show code snippet

const map = new Map()
map.set(document.body, 'blue')
map.set(document.documentElement, 'green')

for (let pair of map) {
  console.log(pair);
}
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Other benefits

There are numerous other benefits why you might pick a Map over other alternatives, like performance as covered by Asthmatic, avoiding Object prototype issues, convenience etc.

Hopefully this generic overview has covered most questions you had.

Answer from Etheryte on Stack Overflow
Top answer
1 of 4
12

As covered briefly by MDN, Map has a number of technical benefits — while a simple Array or an Object may suffice for simple data structures, oftentimes the increasing complexity of modern web development justifies using something else.

Let's look at a few specific examples:

The keys of a Map can be anything

In both modern libraries as well as webapps, memoization is often used to cut down performance costs of repetitive operations. Briefly, memoization is the act of storing the output of a given function for given inputs after the first run.
Maps are a great candidate for storing this data, since you can use them regardless of what you want to index by.

Show code snippet

const cache = new Map();
const input = [2, 3, 2]; // Assuming this is immutable for simplicity

function expensive(n, ...args) {
  if (!args.length) return n;
  return Math.pow(n, expensive(...args));
}

function memoExpensive(arr) {
  const memoResult = cache.get(arr);
  if (memoResult) {
    console.log('used memo', memoResult);
    return memoResult;
  }
  const result = expensive(...arr);
  console.log('calculated', result);
  cache.set(arr, result);
  return result;
}

memoExpensive(input);
memoExpensive(input);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

There are cases where you could also use a Symbol and a regular Object, but a Map is more universal in this regard.

Map keys are ordered

For a regular Object, keys are partially ordered (since ECMAScript 2015, before that it was a bit iffy on different engines). Keys keep their order only for string and Symbol keys, while numerical keys are sorted.
When adding keys to an Object in the order c, b, a, 3, 2, 1, the stored order will be 1, 2, 3, c, b, a, for example.

Show code snippet

const obj = {};
obj.c = 1;
obj.b = 2;
obj.a = 3;
obj[3] = 4;
obj[2] = 5;
obj[1] = 6;
console.log(Object.keys(obj));
Run code snippetEdit code snippet Hide Results Copy to answer Expand

A Map will keep the order of the inserted keys regardless of their type.

Show code snippet

const map = new Map();
map.set('c', 1);
map.set('b', 2);
map.set('a', 3);
map.set(3, 4);
map.set(2, 5);
map.set(1, 6);
console.log(Array.from(map.keys()));
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Oftentimes this won't matter, especially in casual web dev, but there are numerous notorious cases when the order of the keys does matter.

Map is iterable

While an Array, as brought up in the question, is also iterable, a Map offers a better use-case for key-value mappings than an Object would. Being iterable has a number of benefits, just-in-time execution and guaranteed order being two examples.

Show code snippet

const map = new Map()
map.set(document.body, 'blue')
map.set(document.documentElement, 'green')

for (let pair of map) {
  console.log(pair);
}
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Other benefits

There are numerous other benefits why you might pick a Map over other alternatives, like performance as covered by Asthmatic, avoiding Object prototype issues, convenience etc.

Hopefully this generic overview has covered most questions you had.

2 of 4
3

The V8 engine heap isn't very good at managing arrays of objects. If you have an array of 100,000 objects, loop through each object and delete one parameter in the middle then re-add it, the heap size will spike rather than stay constant.

// Spike the memory heap
arr.forEach(obj => {let temp = obj[0]; delete obj[0]; obj[0] = temp})

The reason the heap will spike is because V8 creates intermediate arrays during the forEach loop over the array. Once the array you're looping over becomes massive (eg. 100,000 elements) the memory spike will be too large to handle and could crash the program.

Iterables like Map and Set aren't handled by the engine this way, and you can use a library like iter-tools to get array-like transformation methods for them.

🌐
GitHub
gist.github.com › jung-kim › 83676b2310c7c2a9c3d8
es6 map vs array vs obj performance test · GitHub
do not leave holes in array, when initializing, push the data as they go, where in the original benchmark, the array can have arbitrary gaps, which stops v8 from optimizing. ... Map int key set took: 449 Obj int key set took: 432 ray int key set took: 45 Map int key get took: 31 Obj int key get took: 32 ray int key get took: 33 Map string key set took: 1176 Obj string key set took: 756 ray string key set took: 1234 Map string key get took: 33 Obj string key get took: 439 ray string key get took: 393
Discussions

Javascript performance of Array.map - Stack Overflow
Just wrote up some test cases in jsperf to test the difference between named and anonymous functions while using Array.map and other alternatives. http://jsperf.com/map-reduce-named-functions (ex... More on stackoverflow.com
🌐 stackoverflow.com
Performance of array includes vs mapping to an Object and accessing it in JavaScript - Stack Overflow
So is it more performant to map an array into a dictionary and then access the element directly or should I just use includes? This question is specifically for JavaScript because I believe this would come down to core implementation details of how includes() and {} is implemented. More on stackoverflow.com
🌐 stackoverflow.com
javascript - What is faster? Map get() or Array find()? - Stack Overflow
Im working with javascript but maybe there is also an general answer. Assuming we have 10 objects with ids. Would it be faster to search for an object with an specific id by using the array find() More on stackoverflow.com
🌐 stackoverflow.com
Are maps faster than arrays?
It would also be helpful, if anyone happens to know. If there is any limit to the map size in Unreal, ignoring actual storage capacity. Does the size of a map slow down finding an item in such, or increase memory usage while doing so. More on reddit.com
🌐 r/unrealengine
9
2
July 28, 2022
🌐
Medium
medium.com › @dm_md › unveiling-the-speed-of-javascript-collections-set-vs-map-vs-array-vs-object-3f6e44f24505
Unveiling the Speed of JavaScript Collections: Set vs. Map vs. Array vs. Object | by Dmytro Demchenko | Medium
March 17, 2024 - While performance is important, it’s also crucial to consider the specific requirements of your application. The choice between Set, Map, Array, and Object should be based on the operations you need to perform most frequently, as well as the size of the collection.
🌐
DEV Community
dev.to › henryjw › array-map-much-slower-than-for-loop-57if
Array.map() much slower than for loop - DEV Community
April 29, 2019 - The results were that Array.forEach() is still slower, but not by as much as .map() (550-700ms). My guess is that .map() performs some additional logic that slows it down significantly compared to a raw for loop. Edit: I'm aware that this isn't exactly a practical scenario as we shouldn't be processing this much data using Javascript.
Top answer
1 of 3
44

It's true that object lookup occurs in constant time - O(1) - so using object properties instead of an array is one option, but if you're just trying to check whether a value is included in a collection, it would be more appropriate to use a Set, which is a (generally unordered) collection of values, which can also be looked up in linear time. (Using a plain object instead would require you to have values in addition to your keys, which you don't care about - so, use a Set instead.)

const set = new Set(['foo', 'bar']);
console.log(set.has('foo'));
console.log(set.has('baz'));

This will be useful when you have to look up multiple values for the same Set. But, adding items to the Set (just like adding properties to an object) is O(N), so if you're just going to look up a single value, once, there's no benefit to this nor the object technique, and you may as well just use an array includes test.

2 of 3
15

Updated 04/29/2020

As the commenter rightly pointed out it would seem V8 was optimizing out the array includes calls. An updated version that assigns to a var and uses it produces more expected results. In that case Object address is fastest, followed by Set has and in a distant third is Array includes (on my system / browser).

All the same, I do stand by my original point, that if making micro-optimizations it is worth testing assumptions. Just make sure your tests are valid ;)

Original

Well. Despite the obvious expectation that Object address and Set has should outperform Array includes, benchmarks against Chrome indicate that implementation trumps expectation.

In the benches I ran against Chrome Array includes was far and away the best performer.

I also tested locally with Node and got more expected results. In that Object address wins, followed closely by Set has, then Array includes was marginally slower than both.

Bottom line is, if you're making micro-optimizations (not recommending that) it's worth benchmarking rather than assuming which might be best for your particular case. Ultimately it comes down to the implementation, as your question implies. So optimizing for the target platform is key.

Here's the results I got:

Node (12.6.0):

ops for Object address 7804199
ops for Array includes 5200197
ops for Set has        7178483

Chrome (75.0):
https://jsbench.me/myjyq4ixs1/1

🌐
Medium
medium.com › @bretcameron › how-javascript-maps-can-make-your-code-faster-90f56bf61d9d
How JavaScript Maps Can Make Your Code Faster | by Bret Cameron | Medium
December 11, 2021 - 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…
Find elsewhere
🌐
LogRocket
blog.logrocket.com › home › es6 keyed collections: maps and sets
ES6 keyed collections: Maps and sets - LogRocket Blog
June 4, 2024 - For example, to iterate over an object or to sort it, you had to first convert it to an array, then perform those operations. With arrays, looping was not a problem, but if you needed to pull out a specific value without its index, it was not at all straightforward. Keyed collections — namely, Map, Set, WeakMap, and WeakSet — were introduced to solve these issues and to make working with values or key-value pairs easier.
🌐
CodeBurst
codeburst.io › array-vs-set-vs-map-vs-object-real-time-use-cases-in-javascript-es6-47ee3295329b
Array vs Set vs Map vs Object — Real-time use cases in Javascript (ES6/ES7) | by Rajesh Babu | codeburst
March 28, 2023 - Note: Compared to the array’s includes function, Object’s hasOwnProperty and Set/Map’s has functions seem to perform close to O(1) in different tests, clearly more efficient in terms of larger data sets.
🌐
Felix Gerschau
felixgerschau.com › foreach() vs. map() — javascript array function comparison | felix gerschau
forEach() vs. map() — JavaScript Array Function Comparison | Felix Gerschau
November 11, 2020 - You will use map for changing each value of a given array, while forEach simplifies iterating over an array without changing its values. Do you want to know what the performance impacts are for these two functions?
🌐
Ossph
blog.ossph.org › bite-sized-javascript-array-vs-map-which-is-better
Bite-sized JavaScript: Array vs Map which is better?
March 7, 2023 - Arrays are a good choice for tasks that involve random access to elements, while maps are a good choice for tasks that involve quick lookups of specific values. It is important to consider the speed and functionality of each data structure when ...
🌐
Medium
medium.com › @collettemathieu › map-or-not-map-in-javascript-a-performance-review-46c6fa25fdf8
Map or not Map in JavaScript? A performance review | by Mathieu Collette | Medium
January 14, 2025 - After the initial setup, the cached data in the Map allows for near-instantaneous access, significantly outperforming Array.find() in repeated use cases. ... The dataset is relatively small, or you are performing lookups infrequently.
🌐
Zhenghao
zhenghao.io › posts › object-vs-map
When You Should Prefer Map Over Object In JavaScript
June 18, 2022 - I started with Object and Map with a size of 100 properties/entries, all the way to 5000000, and had each type of operations keep running for 10000ms to see how they performed against each other. Here are my findings... Why do we stop when the number of entries reaches 5000000? Because this is about as big as an object can get in JavaScript.
🌐
Reddit
reddit.com › r/unrealengine › are maps faster than arrays?
r/unrealengine on Reddit: Are maps faster than arrays?
July 28, 2022 -

For the purpose of finding an item.

Is it better to get an array element at x index.

Or is it better to get a map element at x key.

For example let's say you have characters each with health and mana.

Would it be better to have 2 maps, each with their names and values. Or to have 2 arrays with just the values?

Ignore structs and datatables, this is just for the purpose of the question.

TLDR: are maps faster and more efficient than arrays for their purpose, or are they just more practical.

edit: reason why I'd think they are not is cuz maps store a key and a value. Whereas arrays store only list of values. But maybe how they work behind the scenes makes maps somehow better at what they are supposed to be used for.

🌐
Mostly Focused
mostlyfocused.com › pages › articles › maps_or_sets
Should You Use JavaScript Maps And Sets? - mostlyFOCUSED
October 13, 2024 - But the thing about JS performance is that as browsers and run time environments grow, they can choose to boost certain aspects. So in the future it may be absolutely true that Google optimized the heck out of Maps, so use those. I'm just saying that for now, that seems debatable, and not a reason to switch. As for Sets, there can be no debate that Set.has is faster than array.includes (that's O(N) for arrays vs.
🌐
HackerNoon
hackernoon.com › 3-javascript-performance-mistakes-you-should-stop-doing-ebf84b9de951
3 JavaScript Performance Mistakes You Should Stop Doing | HackerNoon
October 6, 2018 - Moreover, client-side javascript is claiming to be a reasonable solution for more than just presentation\view, as end-users computers grow stronger, and networks faster — but can we rely on this when our application requires blazing fast performance and might be a very large and complex one? To test these questions, I tried comparing a few scenarios and drilled down to understand the results I got. I executed the following tests on Node.js v10.11.0 and in the Chrome browser, both on macOS. The first scenario which came to mind was summing an array of 10k items, this is a valid real-life solution I stumbled upon while trying to fetch a long table of items from the database and enhance it with the total sum, without having an additional query to the DB.
🌐
DEV Community
dev.to › ravdz › choosing-between-array-and-map-in-javascript-2bh8
Choosing Between Array and Map in JavaScript - DEV Community
May 6, 2026 - If you just need to list items, filter them, or render them in UI order, an array is simpler. If you often search by ID, toggle specific items, or sync updates efficiently, Map will pay off in performance and clarity.
🌐
Quora
quora.com › What-is-better-a-large-map-or-array-JavaScript
What is better, a large map or array JavaScript? - Quora
Answer (1 of 3): If you’re in need of associative lists then use maps. Arrays in javascript will inherit prototypes which can clash with your key names. Eg, new Array()[“includes”];