Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map.has vs Object hasOwnProperty
(version: 0)
Lookup of map vs object
Comparing performance of:
Map lookup vs Obj lookup
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'); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Map lookup
Obj lookup
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 26_0_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/141.0.7390.41 Mobile/15E148 Safari/604.1
Browser/OS:
Chrome Mobile iOS 141 on iOS 26.0.1
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Map lookup
497526.3 Ops/sec
Obj lookup
668528.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, the options compared, their pros and cons, and other considerations. **What's being tested?** The benchmark tests the performance of two different approaches to check if a key exists in an object: `Map.has()` and `Object.hasOwnProperty()`. The test cases create a large map and object with 10,000 entries each and then look up a specific string key in both data structures. **Options compared** There are two options being compared: 1. **`Map.has()`**: This method is used to check if a key exists in a Map object. 2. **`Object.hasOwnProperty()`**: This method is used to check if a property exists in an object using the `hasOwnProperty()` method, which returns true if the object has the specified property as its own property (i.e., not inherited from the prototype chain). **Pros and Cons** **`Map.has()`** Pros: * Generally faster than `Object.hasOwnProperty()`, especially for large datasets. * Provides a more efficient way to check if a key exists in a Map, reducing the overhead of searching through the object's properties. Cons: * Only available in ECMAScript 2015 (ES6) and later versions. * Requires the use of a Map object, which may be overkill for simple lookup scenarios. **`Object.hasOwnProperty()`** Pros: * Widely supported across different browsers and environments, including older versions. * Can be used with any object type, not just Maps. Cons: * Slower than `Map.has()`, especially for large datasets. * May involve searching through the object's prototype chain to find the property, which can lead to slower performance. **Other considerations** * The use of a string key in both `Map.has()` and `Object.hasOwnProperty()` may introduce additional overhead due to string processing. * The test cases create a large dataset (10,000 entries each), which may exaggerate the differences between these two methods. In practice, smaller datasets might not show significant performance differences. **Library usage** There is no library explicitly mentioned in the benchmark definition or test cases. However, the use of `Map` and `Object` suggests that the test relies on built-in JavaScript objects. **Special JS feature/syntax** None are mentioned in this specific benchmark, but it's worth noting that some newer JavaScript features like `Map.prototype.get()` and `Object.prototype.hasOwnProperty.call()` might be used to optimize these methods further. However, they are not applicable to this particular benchmark definition.
Related benchmarks:
Map has vs get
Array from() vs Map.keys()
Test if key is in object.
entries vs keys lookup
Map vs Obj Lookup
Comments
Confirm delete:
Do you really want to delete benchmark?