Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
Run results for:
Map .has() vs .get()
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);```
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser:
Chrome 124
Operating system:
Linux
Device Platform:
Desktop
Date tested:
one year ago
Benchmark engine:
Benchmark.js
.get() only
1K/s
.has() and .get()
764/s
View exact numbers
Test name
Executions per second
✓
.get() only
1,033 Ops/sec
.has() and .get()
764 Ops/sec
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); })