Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map has vs Object hasOwnProperty
(version: 0)
Comparing performance of: Map lookup vs Obj lookup
Comparing performance of:
Map lookup vs Obj lookup
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var map = new Map(); var obj = {}; map.set('a', 5); obj['a'] = 5; var i = 0, count = 100000;
Tests:
Map lookup
for (i = 0; i < count; i++) { map.has('a'); }
Obj lookup
for (i = 0; i < count; i++) { obj.hasOwnProperty('a'); }
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:
8 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:141.0) Gecko/20100101 Firefox/141.0
Browser/OS:
Firefox 141 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Map lookup
6657.3 Ops/sec
Obj lookup
7238.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and considered. **Benchmark Overview** The benchmark is designed to compare the performance of two different ways to check if a key exists in an object or map: 1. **Map lookup**: Using the `has()` method on a `Map` instance. 2. **Object lookup**: Using the `hasOwnProperty()` method on an object (represented as a plain JavaScript object, not an actual DOM element). **Options Compared** The benchmark is comparing two approaches: * **Map lookup**: Using `map.has('a')` * **Object lookup**: Using `obj.hasOwnProperty('a')` These two methods are designed to achieve the same result: checking if a key exists in the map/object. However, they have different implementations and potential performance implications. **Pros and Cons of Each Approach** 1. **Map Lookup (using `has()` method)**: * Pros: + More concise and readable code. + Likely to be implemented as a single, optimized function call. * Cons: + May have slower performance due to the additional overhead of checking if the key is present in the map. 2. **Object Lookup (using `hasOwnProperty()` method)**: * Pros: + Might be faster due to the optimized implementation of `hasOwnProperty()`. * Cons: + Less concise and readable code compared to `map.has('a')`. **Library Usage** In this benchmark, a `Map` instance is created using `var map = new Map();`. The purpose of a `Map` is to store key-value pairs and provide fast lookups, insertions, and deletions. In this case, the `has()` method on the map is used to check if a specific key (`'a'`) exists. **Special JavaScript Feature** The benchmark uses the **arrow function syntax**, which was introduced in ECMAScript 2015 (ES6). The arrow functions are defined using the `=>` operator and provide a concise way to define small, one-time use functions. In this case, the arrow function is used for simplicity and readability. **Other Alternatives** If you were to implement a similar benchmark, other alternatives could include: * Using the `in` operator instead of `hasOwnProperty()`. * Implementing a custom lookup function using bitwise operations or hashing. * Using a different data structure, such as an array with a custom length property, to store key-value pairs. Keep in mind that these alternatives might introduce additional complexity and potential performance differences.
Related benchmarks:
iterating from a filled object VS iterating from a map
Array from() vs Map.keys()
array includes vs object key lookup, large arrays
Map vs Object read performance for a 1000 key lookup
Object.fromEntries vs Map
Comments
Confirm delete:
Do you really want to delete benchmark?