Do you know the difference between an Object and a Map in JavaScript?

Mohith Gupta
4 min readDec 8, 2023

--

Source: Found it at this link

No intro. Not gonna do the hi-hows-life-heres-mine stuff. Diving right into the topic!

Objects Vs Map in JavaScript

In JavaScript, both objects and maps are used to store collections of key-value pairs, but they have some differences in their behaviour and use cases.

Objects:

Literal Syntax:

  • Objects are created using literal syntax {} or new Object().

Keys:

  • Keys in objects can be strings or symbols (from ES6 onwards).
  • They are primarily used for storing non-iterable properties and methods.

Usage:

  • Widely used for storing data and creating complex structures and is also commonly used in everyday JavaScript for general-purpose data storage and manipulation. (I use them a lot, a looooot!)

Iteration:

  • Objects do not have built-in methods for iteration, so you’d typically use for...in loops or methods like Object.keys(), Object.values(), or Object.entries() to iterate through keys, values, or entries.
const obj = { name: 'John', age: 30 };

Maps:

Constructor:

  • Maps are created using the Map constructor or by using the new Map() syntax.

Keys:

  • Keys in maps can be any data type, including objects.
  • Maps maintain the order of elements based on insertion, unlike objects.

Usage:

  • Maps are suitable for scenarios where keys are not limited to strings and when the order of elements matters.
  • They provide better support for scenarios where iteration or maintaining insertion order is crucial.

Iteration:

  • Maps have built-in methods for iteration like forEach, entries(), keys(), values(), making it easier to loop through keys, values, or entries in the map.

Example:

const map = new Map();
map.set('name', 'John');
map.set('age', 30);

// You cannot assign values like this: map["name"]= "John"
// we should always use .set() method

Choosing Between Object and Map:

  • Use objects when you have a simple key-value data structure and keys are mainly strings or symbols.
  • Use maps when you need flexibility in key types, maintaining insertion order, or when you require built-in iteration methods.

In general, the performance difference between objects and maps in JavaScript might not be substantial for most use cases. However, there are scenarios where one might perform better than the other based on the specific operations being performed but these are mostly insignificant unless the application requirements are very sensitive to performance.

Performance Considerations:

Access Speed:

  • Objects might have slightly faster access times because they use hash tables for key lookup, resulting in constant time complexity (O(1)) for property access.
  • Maps, while still having good performance, might be marginally slower due to their flexibility with different key types. You might see a difference in access speed if the pairs in the data structure increase exponentially, should be fine otherwise.

Iteration:

  • Maps have built-in iterable features, making them more optimized for iteration over keys, values, or entries.
  • For objects, iterating might involve additional steps like extracting keys using Object.keys() or similar methods, which might impact performance slightly, especially in large collections.

Summary:

  • For scenarios, where the keys are primarily strings or symbols and simple key-value storage, is required, objects might offer a slight advantage in terms of performance due to their optimized property access.
  • In cases where key types vary, insertion order matters, or extensive iteration is required, maps offer better performance due to their built-in iteration methods and flexibility in key types.
  • The choice between objects and maps should primarily consider the data structure’s suitability for the intended use case rather than minor performance differences.
  • Optimizing for readability, maintainability, and code clarity should also be considered alongside micro-optimizations based on performance.
// Objects might be slightly faster for simple string-key access:

const obj = { key1: 'value1', key2: 'value2' };
const value = obj['key1'];

// Maps might be more suitable for diverse key types and iteration:

const map = new Map();
map.set('key1', 'value1');
map.set(1, 'value2');

map.forEach((value, key) => {
// Perform operations on key-value pairs with in-built methods
});

Let me know in the comments if I have left out anything important, I’ll add it in. That’s it!

Please consider showing your appreciation by providing feedback or suggestions in the comments and if you are pleased too much, you can always buy me a coffee 😉

Thanks for reading till the end! Adios!

You can read some of my other popular posts:

--

--

Mohith Gupta

A Better Person than Yesterday, I guess? In the process of Learning