A technical blog on Computer Science and Software Development topics by Tomás Pérez.

Maps in Harmony

In old-school Javascript key/value storages can be defined using simple objects {}, however performance wise have some drawbacks compared to the new data structures present in Harmony: Maps and Sets.

The Map object is a simple key/value map, any value (objects or primitives) can be used as either a key or a value.

Iteration of elements in insertion order is supported in opposite to the traditional iteration on objects for var in where traversal order was not guaranteed. That's not the only difference compared with objects:

In a Map, keys can be any value (in Objects they should be strings).

You can determine easily the size of Maps (i.e. the number of elements) using the property size:

var map = new Map();
map.set('key', 25);
console.log(map.size); // => '1'

Checking existence of a key and deleting specific values:

map.has('key'); // => true
map.has('key'); // => false

Iterating on its keys or values using the Iterator returned by the methods keys and values.

map.set('key-1', 1);
map.set('key-2', 2);
var iterator = map.values();
console.log(; // => 1
console.log(; // => 2

A more generic way is using the method entries that will return an Iterator as well but we will be able to process keys and values at the same time:

var iterator = map.entries();
console.log([0]); // => 'key-1'
console.log([1]); // => 1

Similar to entries, we can use the loop for...of:

for (var current of map.entries()) { 
  console.log(current[0]); // => 'key-1'
  console.log(current[1]); // => 1

Another way of traversing the elements of a Map is using the `forEach' method:

map.set('key-1', 1);
map.set('key-2', 2);
map.forEach(function(value, key) {


Mozilla Developer Network: Map object
Pitfalls of usin an object as a map