Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map.has vs Map.get check if the key exists
(version: 0)
Comparing performance of:
has vs get
Created:
2 years ago
by:
Guest
Go to the latest result
Tests:
has
const map = new Map(); map.set("foo", 1); for(let i = 0; i < 10000; i ++){ if(map.has("foo")){ } }
get
const map = new Map(); map.set("foo", 1); for(let i = 0; i < 10000; i ++){ if(map.get("foo")){ } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
has
get
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36
Browser/OS:
Chrome 125 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
get
139K/s
has
37K/s
View exact numbers
Test name
Executions per second
✓
get
139,337 Ops/sec
has
37,283 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks and analyze what's being tested on MeasureThat.net. **Benchmark Overview** The benchmark in question is designed to compare two approaches for checking if a key exists in a Map object: `map.has()` and `map.get()`. A Map is a data structure that stores mappings between keys and values. In this case, the map contains only one key-value pair, "foo" with value 1. **Options Compared** The benchmark tests two options: 1. **`map.has('foo')`**: This approach checks if the key 'foo' exists in the map using the `has()` method. 2. **`map.get('foo')`**: This approach attempts to retrieve the value associated with the key 'foo' using the `get()` method. **Pros and Cons of Each Approach** **`map.has('foo')`** Pros: * More efficient since it only checks if the key exists, without retrieving its associated value. * Can be faster for large maps or when only checking existence. Cons: * May not work as expected if the map is modified concurrently (e.g., another thread inserts a new key). * Returns `false` if the map is empty, whereas `map.get()` returns `undefined`. **`map.get('foo')`** Pros: * Works consistently even when the map is modified concurrently. * Returns `undefined` if the map does not contain the key. Cons: * May be slower since it retrieves the associated value, even if only to check existence. * Can lead to unnecessary memory allocation and garbage collection if the retrieved value is not used. **Other Considerations** When using `map.has('foo')`, consider that this method may not work as expected if the map's underlying data structure is modified concurrently. In contrast, `map.get('foo')` is more robust in this regard but might be slower due to unnecessary retrievals. If you need to frequently check for key existence without retrieving its associated value, `map.has('foo')` might be a better choice. However, if you want to ensure consistent behavior even when modifying the map concurrently, or need to retrieve the associated value occasionally, `map.get('foo')` is likely more suitable. **Library and Special JS Feature** In this benchmark, no external library is used beyond JavaScript's built-in `Map` data structure. There are no special JavaScript features or syntaxes involved, so you can easily reproduce this benchmark using a standard web browser or Node.js environment with the JavaScript engine enabled. **Other Alternatives** Alternatives to measure execution time for small scripts like this include: * Browser developer tools (e.g., Chrome DevTools' Benchmarking panel) * Node.js `performance.now()` function * Online testing platforms like CodePen, JSFiddle, or Repl.it Keep in mind that these alternatives might not provide the same level of control as MeasureThat.net's custom benchmarking environment.
Related benchmarks
null vs. hasOwnProperty
new Set([x]).has vs [x].includes
Comments
Confirm delete:
Do you really want to delete benchmark?