Unveiling the Speed of JavaScript Collections: Set vs. Map vs. Array vs. Object

Dmytro Demchenko
4 min readMar 17, 2024

--

In the dynamic and ever-evolving landscape of web development, performance is a critical factor that can significantly impact the user experience. JavaScript, being the backbone of modern web applications, offers a variety of data structures to manage collections of data. Among these, Set, Map, Array, and Object are the most commonly used. Each of these has its unique characteristics and use cases. However, when it comes to performance, especially in terms of speed and memory usage, the differences between them become crucial. In this post, we'll delve into the performance aspects of Set, Map, Array, and Object in JavaScript, comparing their speed and efficiency.

Performance Comparison

Set vs. Array

  • Memory Usage: Set tends to use more memory than Array because it needs to store additional information to maintain the hash table. However, the difference is usually negligible for small collections.
  • Speed: Set is generally faster than Array for operations that involve checking the presence of an element, such as has and add. This is because Set uses a hash table internally, which allows for constant time complexity (O(1)) for these operations, whereas Array requires linear time complexity (O(n)) to find an element.
  
function benchmark(fn, iterations) {
const start = performance.now();
for (let i = 0; i < iterations; i++) {
fn();
}
const end = performance.now();
return end - start;
}

function benchmarkAdditionSet() {
const set = new Set();
for (let i = 0; i < 100000; i++) {
set.add(i);
}
}

function benchmarkAdditionArray() {
const array = [];
for (let i = 0; i < 100000; i++) {
array.push(i);
}
}

function benchmarkRemoveSet() {
const set = new Set();
for (let i = 0; i < 100000; i++) {
set.add(i);
}
for (let i = 0; i < 100000; i++) {
set.delete(i);
}
}

function benchmarkRemoveArray() {
const array = [];
for (let i = 0; i < 100000; i++) {
array.push(i);
}
for (let i = 0; i < 100000; i++) {
const index = array.indexOf(i);
if (index > -1) {
array.splice(index, 1);
}
}
}

console.log('Set addition time:', benchmark(benchmarkAdditionSet, 10)); // 30.699999809265137 ms
console.log('Array addition time:', benchmark(benchmarkAdditionArray, 10)); // 2.9000000953674316 ms
console.log('Set removal time:', benchmark(benchmarkRemoveSet, 10)); // 63.700000047683716 ms
console.log('Array removal time:', benchmark(benchmarkRemoveArray, 10)); // 5597.299999952316 ms

Map vs. Object

  • Speed: Map is generally faster than Object for operations that involve frequent additions and removals of key-value pairs. This is because Map maintains an internal order of its elements, which allows for faster iteration and access to elements. Object does not maintain an order, making these operations slower.
  • Memory Usage: Map uses more memory than Object because it needs to store additional information to maintain the order and handle key-value pairs efficiently. However, for most use cases, the difference is minimal.
  /** Create a Map */
const myMap = new Map();
for (let i = 0; i < 1000000; i++) {
myMap.set(i, `Value ${i}`);
}

/** Create an Object */
const myObject = {};
for (let i = 0; i < 1000000; i++) {
myObject[i] = `Value ${i}`;
}

/** Measure the time taken to iterate over the Map */
console.time("Map Iteration");
for (const [key, value] of myMap) {
// Do something with the key and value
}
console.timeEnd("Map Iteration");

/** Measure the time taken to iterate over the Object */
console.time("Object Iteration");
for (const key in myObject) {
if (myObject.hasOwnProperty(key)) {
// Do something with the key and value
}
}
console.timeEnd("Object Iteration");

// Results
// Map Iteration: 21.83984375 ms
// Object Iteration: 86.773193359375 ms

Set vs. Map

  • Speed: Set is faster than Map for operations that involve only keys, such as checking the presence of a key. Map is optimized for key-value pairs, making it faster for operations that involve both keys and values.
  • Memory Usage: Set uses less memory than Map because it only needs to store keys. Map needs to store both keys and values, leading to higher memory usage.

Practical Considerations

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.

  • Use Set when you need to store unique values and perform fast lookups.
  • Use Map when you need to associate values with keys and perform fast lookups based on keys.
  • Use Array when you need to maintain an ordered collection of items and perform operations that benefit from the array's index-based access.
  • Use Object when you need a simple key-value store and don't require the additional features provided by Map.

Conclusion

Understanding the performance characteristics of Set, Map, Array, and Object in JavaScript is crucial for making informed decisions about which data structure to use in your applications. While Set and Map offer performance advantages for certain operations, the choice should always be guided by the specific needs of your application. By carefully considering the trade-offs between speed, memory usage, and functionality, you can optimize your application's performance and ensure a smooth user experience.

--

--