How fast is javascript map?

Pricing

Dominik Moritzwww.domoritz.de

Faculty at CMU (@cmudig) and researcher at @apple.

Published

1 Like

Insert cell

Insert cell

Insert cell

Insert cell

Insert cell

Insert cell

Insert cell

Insert cell

Insert cell

Insert cell

Insert cell

Create interactive documents like this one.

Learn new data visualization techniques. Perform complex data analysis.
Publish your findings in a compelling document. All in the same tool.

More from Observable creators

node version : v4.1.2
platform: Intel I7, 16G DDR3, Ubuntu x64

var theSet = new Set();
start = process.hrtime();
/***********************************/
for(let i = 0 ; i < N; i++ ) {
    let v = i%20;
    if (!theSet.has(v))
        theSet.add(v);
}
/***********************************/
console.log('section 1 : ' + timeUse(start));


obj = {};
start = process.hrtime();
/***********************************/
for(let i = 0 ; i < N; i++) {
    let v = i%20;
    if (!obj[v])
        obj[v] = 1;

}
/***********************************/
console.log('section 2 : ' + timeUse(start));

result :

# N = 1M
section 1 : 28348894ns
section 2 : 13017222ns

# N = 1K
section 1 : 1616666ns
section 2 : 109862ns

conclusion: If unnecessary, use Object instead of Set for performance benefit.

Map vs Object

var themap = new Map();
start = process.hrtime();
/***********************************/
for(let i = 0 ; i < N; i++ ) {
    let v = i%20;
    if (!themap.get(v))
        themap.set(v, i);
}
/***********************************/
console.log('section 1 : ' + timeUse(start));


obj = {};
start = process.hrtime();
/***********************************/
for(let i = 0 ; i < N; i++) {
    let v = i%20;
    if (!obj[v])
        obj[v] = 1;
}
/***********************************/
console.log('section 2 : ' + timeUse(start));

result:

# N = 1M
section 1 : 27290298ns
section 2 : 12362120ns

# N = 1K
section 1 : 1749059ns
section 2 : 63746ns

conclusion: same as Set vs Object

The backing store for a Map.

The FastMap is a map from arbitrary keys to values. It is itself backed by a FastSet of [key, value] entries, with contentEquals and contentCompare overridden to consider only the key. The FastMap is fast because it does not track insertion order of the entries, so it will not behave exactly like an ECMAScript 6 Map in that regard. The enumeration order of the map will depend on whether there are hash collisions. If there are no hash collisions, its enumeration order is consistent with Map.

Properties

length

The number of items in this collection.

Methods

has(key)

Returns whether an entry with the given key exists in a Map.

get(key|index, default?)

Gets the value for a key in a map.

set(key, value)

Sets the value for a given key.

add(value, key)

Adds a value for a given key to a map.

delete(key)

Deletes the value for a given key. Returns whether the key was found and successfully deleted.

keys()

Returns an array of the keys of this map.

values()

Returns an array of the values of this map.

entries()

Returns an array of all [key, value] entries for this map.

addEach(values|map)

Copies values or entries from another collection into this collection, and then returns this.

deleteEach(values|keys, equals?)

Deletes every value or every value for each key. Returns the number of successful deletions.

clear()

Deletes all of the values in the collection.

iterate|iterator()

Iterates every value in this collection.

forEach(callback, thisp?)

Calls the callback for each entry in the collection.

map(callback, thisp?)

Returns an array of the respective return values of a callback for each entry in this collection.

filter(callback, thisp?)

Returns an array with each value from this collection that passes the given test.

reduce(callback, basis)

Aggregates every value in this collection with the result collected up to that index.

reduceRight(callback, basis)

Aggregates every value in this collection, from right to left.

group(callback, thisp?, equals?)

Returns an array of [key, class] entries where every value from the collection is placed into the same equivalence class if they return the same key through the given callback.

some(callback, thisp?)

Returns whether any entry in this collection passes a given test.

every(callback, thisp?)

Returns whether every entry in this collection passes a given test.

any()

Returns whether any value in the collection is truthy.

all()

Returns whether all values in the collection are truthy.

only()

Returns the only value in this collection, or undefined if there is more than one value, or if there are no values in the collection.

sorted(compare?)

Returns a sorted array of the values in this collection.

reversed()

Returns a copy of this collection with the values in reverse order.

join(delimiter?)

Returns a string of all the values in the collection delimited by the given string.

sum(zero?)

Returns the sum of all values in this collection.

average()

Returns the arithmetic mean of the collection, by computing its sum and the count of values and returning the quotient.

min()

Returns the smallest value in this collection.

max()

Returns the largest value in this collection.

zip(...iterables)

Returns an array of the respective values in this collection and in each collection provided as an argument.

enumerate(start?)

Returns an array of [index, value] entries for each value in this collection, counting all values from the given index.

concat(...iterables)

Returns a new collection of the same type containing all the values of itself and the values of any number of other iterable collections in order.

flatten()

Assuming that this is a collection of collections, returns a new collection that contains all the values of each nested collection in order.

toArray()

Returns an array of each value in this collection.

toObject()

Returns an object with each property name and value corresponding to the entries in this collection.

toJSON()

Used by JSON.stringify to create a JSON representation of the collection.

equals(value, equals?)

Returns whether this collection is equivalent to the given collection.

clone(depth?, memo?)

Creates a deep replica of this collection.

constructClone(values?)

Creates a shallow clone of this collection.

contentEquals(left, right)

The equals function used to check whether values in this collection are equivalent.

contentHash(value)

The hash function used by this collection to hash its own values.

addMapChangeListener(listener, token?, beforeChange?)

Adds a listener for when the value for a key changes, or when entries are added or removed.

addBeforeMapChangeListener(listener, token?)

Adds a listener for before map entries are created, deleted, or updated.

removeMapChangeListener(listener, token?, beforeChange?)

Unregisters a map change listener provided by addMapChangeListener.

removeBeforeMapChangeListener(listener, token?)

Unregisters a map change listener provided by addBeforeMapChangeListener or addMapChangeListener with the beforeChange flag.

dispatchMapChange(key, value, beforeChange?)

Informs map change listeners that an entry was created, deleted, or updated.

dispatchBeforeMapChange(key, value)

Informs map change listeners that an entry will be created, deleted, or updated.

addOwnPropertyChangeListener(key, listener, beforeChange?)

Adds a listener for an owned property with the given name.

addBeforeOwnPropertyChangeListener(name, listener)

Adds a listener for before a property changes.

removeOwnPropertyChangeListener(name, listener, beforeChange?)

Unregisters a property change listener provided by addOwnPropertyChangeListener.

removeBeforeOwnPropertyChangeListener(key, listener)

Unregisters a property change listener provided by addBeforeOwnPropertyChangeListener or addOwnPropertyChangeListener with the beforeChange flag.

dispatchOwnPropertyChange(key, value, beforeChange?)

Informs property change listeners that the value for a property name has changed.

dispatchBeforeOwnPropertyChange(key, value)

Informs property change listeners that the value for a property name will change.

makePropertyObservable(name)

May perform internal changes necessary to dispatch property changes for a particular name.

Usage

var FastMap = require("collections/fast-map");
  • FastMap()
  • FastMap(values)
  • FastMap(values, equals)
  • FastMap(values, equals, hash)
  • FastMap(values, equals, hash, getDefault)

Related

  • Map

    A map of [key, value] entries, where keys may be arbitrary values including objects.

  • FastSet

    The backing store for Set and FastMap.

Source code

Is JavaScript map faster than object?

numeric keys random(). toString() . The results are similar to those string-key cases: Map s start off as much faster than objects (2 times faster for insertion and deletion, 4-5 times faster for iteration), but the delta is getting smaller as we increase the size.

Is JavaScript map faster than for loop?

map() works way faster than for loop. Considering the same code above when run in this ide.

Is map slower than for loop JavaScript?

We have fahrenheits array of arrays of double and we want to make the array of double. View the code on Gist. The result is very similar to reduce function. The built-in flatMap function is a little bit slower than the for-in loop.

What is the time complexity of map in JavaScript?

Data Structures Big-O Cheatsheet.