var map = new Map();
for (let i = 0; i < 1000; i++) {
map.set(i, {});
}
for(const key of map.keys()) {
map.delete(key);
}
for(const key of map.keys()) {
map.set(key, null);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Map.delete(key) | |
Map.set(key, null) |
Test name | Executions per second |
---|---|
Map.delete(key) | 22506002.0 Ops/sec |
Map.set(key, null) | 23235830.0 Ops/sec |
I'd be happy to explain the benchmark and its results.
Benchmark Overview
The benchmark compares two approaches for updating a Map in JavaScript: map.set(key, null)
vs map.delete(key)
. The goal is to measure which approach is faster.
Options Compared
Two options are compared:
map.set(key, null)
: This approach uses the set()
method of the Map interface to set the value associated with a key to null
.map.delete(key)
: This approach uses the delete()
method of the Map interface to remove an entry from the map.Pros and Cons
map.set(key, null)
:map.delete(key)
.map.delete(key)
:delete()
on Maps.Other Considerations
Benchmark Preparation Code
The script preparation code creates an empty Map map
and sets 1000 key-value pairs using map.set(i, {})
. The HTML preparation code is not provided, which suggests that the test doesn't require any additional setup or rendering elements.
Individual Test Cases
There are two individual test cases:
Map.delete(key)
: This test case runs a loop over all keys in the map and deletes each key using map.delete(key)
.Map.set(key, null)
: This test case runs a loop over all keys in the map and sets each key to null
using map.set(key, null)
.Latest Benchmark Result
The latest benchmark result shows that:
Map.set(key, null)
: Executes approximately 23235830 times per second.Map.delete(key)
: Executes approximately 22506002 times per second.This suggests that map.set(key, null)
is slightly faster than map.delete(key)
. However, please note that these results might not be representative of your specific use case or environment.