Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
Map .has() vs .get()
(version: 3)
Benchmark.js
This is to compare the cost of this pattern ```if (map.has(key)) func1(map.get(key))``` v/s ```const v = map.get(key); if (v) func1(v);```
Comparing performance of:
.has() and .get() vs .get() only
Created:
8 years ago
by:
Registered User
Go to the latest result
Script Preparation code:
function noop() {} map = new Map(); keys = []; for (let i = 0; i < 10000; i++) { const k = new Number(i); keys.push(k); if (k % 2 !== 0) map.set(k, i); }
Tests:
.has() and .get()
keys.forEach(k => { if (map.has(k)) noop(map.get(k)); })
.get() only
keys.forEach(k => { const v = map.get(k); if (v) noop(v); })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
.has() and .get()
.get() only
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:146.0) Gecko/20100101 Firefox/146.0
Browser/OS:
Firefox 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
.get() only
2K/s
.has() and .get()
1K/s
View exact numbers
Test name
Executions per second
✓
.get() only
1,640 Ops/sec
.has() and .get()
1,259 Ops/sec
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark on MeasureThat.net compares two ways to check if a key exists in a `Map` object in JavaScript and then execute a function with the associated value: **Approach 1: `.has()` and `.get()`** ```javascript keys.forEach(k => { if (map.has(k)) noop(map.get(k)); }); ``` * **Explanation:** This approach first uses the `.has()` method to check if the key exists in the `Map`. If it does, the `.get()` method is called to retrieve the associated value, which is then passed to the `noop()` function. **Approach 2: `.get()` only** ```javascript keys.forEach(k => { const v = map.get(k); if (v) noop(v); }); ``` * **Explanation:** This approach directly uses the `.get()` method to retrieve the value associated with each key. If the key exists, `map.get(k)` returns the value; otherwise, it returns `undefined`. The `if (v)` statement then checks if a value was retrieved (not undefined), and only executes `noop(v)` if so. **Pros and Cons:** * **Approach 1: `.has()` and `.get()`** * **Pro:** Potentially more readable, as it explicitly shows the check for key existence before retrieving the value. * **Con:** Might be slightly slower due to the extra method call (`map.has(k)`). * **Approach 2: `.get()` only** * **Pro:** Potentially faster, as it avoids an extra method call. * **Con:** Less explicit about checking for key existence; the `if (v)` statement might be less clear to some readers. **Other Considerations:** * The benchmark results may vary depending on factors such as the size of the map, the specific browser and JavaScript engine being used, and the hardware configuration. * In most real-world scenarios, the performance difference between these two approaches would likely be negligible. Let me know if you'd like to explore other benchmarking concepts or have more questions!
Related benchmarks
Map has vs get
Map vs WeakMap (real-world) Performance
Map vs Object (real-world) Performance (lookup and set)
Comments
Confirm delete:
Do you really want to delete benchmark?