GUIDES Optimizing Embedded JavaScript Map versus Object
About
Binary Data
Index
Convert String to ArrayBuffer
Convert ArrayBuffer to String
Convert ArrayBuffers to String
Handle Errors Converting ArrayBuffer to String
Immutable ArrayBuffers
Resize an ArrayBuffer
Combine ArrayBuffers
Convert Base64 to Binary Data
Convert Binary Data to Base64
Convert Binary Data to Hex
Convert Hex to Binary Data
Calculate CRC for Binary Data
Compress Binary Data – One Buffer
Compress Binary Data – Streaming
Decompress Binary Data – One Buffer
Decompress Binary Data – Streaming
Callbacks
Index
One-Time Callback
Repeating Callback
Repeating Callback with Initial Delay
Immediate Callback
Reschedule Callback
Cancel Callback
Suspend Callback
What About setTimeout?
Optimizing Embedded JavaScript
Index
When to Optimize
Know Where to Optimize
Looping through an Array
Iterating Over a String
Building a String
Avoid Copying Buffers
Accessing Properties
Map versus Object
Appending to an Array
Operating on Bits
Defining Class Methods
Reducing Stack Use
Time
Index
Get Unix Time
Get Time of Day
Get Date
Get Time Since System Start
Get Microseconds
Set System Date and Time
Set Real-Time Clock Time
Get Time and Date from Network
Get Time and Date from Real-Time Clock
Sleep

Map versus Object

You can use both Objects and Maps for collections of items indexed by a key.

Maps are relatively new to JavaScript (over 10 years now!) so it is still common to see Objects used as a map.

// using Object
let obj = new Object();
obj["one"] = 1;
obj["two"] = 2;
obj["three"] = 3;

// using Map
let map = new Map();
map.set("one", 1);
map.set("two", 2);
map.set("three", 3);

Maps are specified to take a more-or-less constant time to access an element. That means that adding, looking up, and deleting items in a map should always take a relatively short period of time. Objects have no such requirement. This means that in Embedded JavaScript you will likely get more predicable performance from a Map than an object. But, to provide this performance guarantee maps use a bit more memory per element than an object.

Which should you use? If the number of items is relatively small, say a few dozen, use whatever you are most comfortable coding. If the number of items may be large, a Map is probably your best bet. As always, the best way to be certain is to try both and measure the performance and memory use.

For a collection of items that is not indexed by a key, scripts commonly use Array. A good alternative is Set which has similar tradeoffs to using a Map instead of an Object. An additional benefit of Set is that removing an item requires less code.

// Using Array
let array = new Array();
array.push("one", "two", "three");

let index = array.indexOf("two");
if (index >= 0)
	array.splice(index, 1); 
	
let hasThree = array.includes("three");
// => hasThree === true

// Using Set
let set = new Set();
set.add("one");
set.add("two");
set.add("three");

set.delete("two");

let hasThree = set.has("three");
// => hasThree === true