const REPEAT = 1000000
const set = new Set()
for (let i = 0; i < REPEAT; i++) {
if (!set.has(`${i}`)) {
set.add(`${i}`)
}
}
const REPEAT = 1000000
const set = new Map()
for (let i = 0; i < REPEAT; i++) {
if (!set.has(`${i}`)) {
set.set(`${i}`, true)
}
}
const REPEAT = 1000000
const set = {}
for (let i = 0; i < REPEAT; i++) {
if (!set[`${i}`]) {
set[`${i}`] = true
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Set | |
Map | |
Object |
Test name | Executions per second |
---|---|
Set | 7.3 Ops/sec |
Map | 6.3 Ops/sec |
Object | 19.9 Ops/sec |
Let's break down the benchmark and explain what's being tested, compared, and their pros and cons.
Benchmark Overview
The benchmark tests the performance of three data structures in JavaScript: Sets, Maps, and Objects. The goal is to determine which one performs best in terms of execution speed.
Data Structures Compared
add()
method.set()
method.[]
).Options Compared
The options being compared are:
add()
methodset()
method[]
)Pros and Cons of Each Approach
Cons:
Library or Special JS Feature Used
None of the benchmark definitions explicitly use any libraries or special JavaScript features beyond the standard Set, Map, and Object APIs.
Alternative Alternatives
If you're looking for alternative data structures in JavaScript, consider:
These alternatives may offer improved performance or flexibility for specific use cases, but they require careful consideration and testing to ensure compatibility with your existing codebase.