Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
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 (Windows NT 10.0; Win64; x64; rv:146.0) Gecko/20100101 Firefox/146.0
Browser:
Firefox 146
Operating system:
Windows
Device Platform:
Desktop
Date tested:
4 months ago
Test name
Executions per second
.has() and .get()
1258.6 Ops/sec
.get() only
1639.7 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); })