Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map.has vs Object hasOwnProperty vs direct check v2
(version: 0)
Lookup of map vs object
Comparing performance of:
Map lookup vs Obj lookup vs direct check
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var map = new Map(); var obj = {}; var j = 0; while (j < 10000) { var key = "Key_" + j; map.set(key, 5); obj[key] = 5; j += 1; } var stringKey = 'asdfasdjfa;sjfjaklfjklsjklajjjj;jlsjfajdfsfjs;fjsfka'; var i = 0, count = 1000, a;
Tests:
Map lookup
for (i = 0; i < count; i++) { a = map.has('stringKey'); }
Obj lookup
for (i = 0; i < count; i++) { a = obj.hasOwnProperty('stringKey'); }
direct check
for (i = 0; i < count; i++) { a = obj['stringKey'] != null }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Map lookup
Obj lookup
direct check
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0
Browser/OS:
Chrome 138 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Map lookup
3164.4 Ops/sec
Obj lookup
3033.0 Ops/sec
direct check
3247.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring the performance of different approaches to checking for key existence in objects is an interesting benchmark. The provided JSON represents a JavaScript microbenchmark that compares three methods: 1. `Map.has()`: 2. `Object.hasOwnProperty()` 3. Direct check (`obj['stringKey'] != null`) **Options being compared:** * `Map.has(key)`: A method introduced in ECMAScript 2015, which checks if a key exists in the map. * `obj.hasOwnProperty('stringKey')`: An older method that checks if the object has the specified property as its own property (not inherited from prototype chain). * Direct check (`obj['stringKey'] != null`): A simple approach that checks if the property exists and is not null. **Pros and cons of each approach:** 1. **Map.has(key)**: * Pros: * More concise and expressive than older methods. * Less prone to false positives (e.g., when using `in` operator). * Cons: * Only available in ECMAScript 2015+ browsers and environments. * May not work as expected in older browsers or with some libraries. 2. **Object.hasOwnProperty('stringKey')**: * Pros: * Widely supported across older browsers and environments. * Cons: * More verbose than `Map.has()` or direct check. * Can return false positives (e.g., when using `in` operator). 3. **Direct check (`obj['stringKey'] != null`)**: * Pros: * Simple and straightforward. * Works in all browsers and environments. * Cons: * May not be as expressive or concise as other methods. **Library usage:** None mentioned in the benchmark definition. The libraries used are built-in JavaScript functions (`Map` and `Object`) and a custom variable (`map`). **Special JS feature/syntax:** There is no special feature or syntax used in this benchmark, but it's worth noting that `Map.has(key)` requires ECMAScript 2015 support. **Other alternatives:** 1. **Using the `in` operator**: This can be used to check if a key exists in an object, but it has its own set of false positives and limitations. 2. **Using bracket notation (`obj['stringKey']`)**: Similar to direct check, but may return null if the property does not exist. When choosing between these alternatives, consider factors such as browser support, performance requirements, readability, and potential for false positives or negatives.
Related benchmarks:
isEmptyObject: keys vs for in
Object.values vs for in loop vs for loop v2
Map vs Object read performance for a 1000 key lookup
Map vs Object (real-world) Performance (better)
Map vs Object (real-world) Performance (non-numeric key)
Comments
Confirm delete:
Do you really want to delete benchmark?