var array = [];
var set = new Set();
var map = new Map();
var existingItem1 = {};
var existingItem2 = {};
var existingItem3 = {};
var newItem1 = {};
var newItem2 = {};
var newItem3 = {};
for (let i = 0, item; i < 100; i++) {
item = {index: i};
array.push(item);
set.add(item);
map.set(item, 0);
}
array.push(existingItem1, existingItem2, existingItem3);
set.add(existingItem1); set.add(existingItem2); set.add(existingItem3);
map.set(existingItem1, 0); map.set(existingItem2, 0); map.set(existingItem3, 0);
array.push(existingItem1);
array.push(newItem1);
array.push(existingItem2);
array.push(newItem2);
array.push(existingItem3);
array.push(newItem3);
set.add(existingItem1);
set.add(newItem1);
set.add(existingItem2);
set.add(newItem2);
set.add(existingItem3);
set.add(newItem3);
map.set(existingItem1, 0);
map.set(newItem1, 0);
map.set(existingItem2, 0);
map.set(newItem2, 0);
map.set(existingItem3, 0);
map.set(newItem3, 0);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
conditional array.push | |
set.add | |
map.set overwrite |
Test name | Executions per second |
---|---|
conditional array.push | 2104078.8 Ops/sec |
set.add | 2211196.0 Ops/sec |
map.set overwrite | 2245524.5 Ops/sec |
Measuring the performance of different JavaScript operations is crucial for optimizing code and ensuring fast execution. Let's break down what's being tested in this benchmark.
Benchmark Overview
The benchmark compares three methods:
array.push()
: adding an element to the end of an arrayset.add()
: adding an element to a Set data structuremap.set()
: setting the value for a key in a Map data structureTest Cases
There are three test cases:
Options Compared
The options being compared are:
array.push()
: adding an element directly to the end of an arrayset.add()
: adding an element to a Set, which maintains uniquenessmap.set()
: setting a value for a key in a MapPros and Cons
Here's a brief summary of the pros and cons of each option:
Library Usage
The Set
and Map
data structures are part of the JavaScript Standard Library. They provide efficient ways to store and manipulate collections of unique values or key-value pairs, respectively.
Special JS Features/Syntax
This benchmark does not explicitly use any special JavaScript features or syntax beyond standard ECMAScript 2020 (ES12) syntax.
Other Alternatives
For large datasets or high-throughput scenarios, alternative data structures like:
array.fill()
: filling an array with a repeating valueset.fromArray()
and map.from()
: creating a Set or Map from an existing collectionmay be more suitable. Additionally, optimized libraries like fast-set
(for Sets) or fast-map
(for Maps) may offer better performance.
Benchmark Conclusion
This benchmark measures the performance of different JavaScript operations for adding elements to arrays, Sets, and Maps. The results provide insights into the trade-offs between simplicity, memory usage, and performance for each data structure. By understanding these pros and cons, developers can make informed decisions about which approach to use in their own code.