Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map vs Object property check
(version: 0)
Comparing performance of:
obj.hasOwnProperty vs map.get vs obj.a vs map.has
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var map = new Map(); var obj = {}; var i = 0, count = 1000, a; for (i = 0; i < count; i++) { map.set('a', 5); } for (i = 0; i < count; i++) { obj['a'] = 5; }
Tests:
obj.hasOwnProperty
for (i = 0; i < count; i++) { a = obj.hasOwnProperty('a'); }
map.get
for (i = 0; i < count; i++) { a = map.get('a'); }
obj.a
for (i = 0; i < count; i++) { a = obj.a; }
map.has
for (i = 0; i < count; i++) { a = map.has('a'); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
obj.hasOwnProperty
map.get
obj.a
map.has
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
obj.hasOwnProperty
10069.3 Ops/sec
map.get
10323.3 Ops/sec
obj.a
10590.8 Ops/sec
map.has
10252.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its components. **Benchmark Overview** The benchmark is designed to compare the performance of three different approaches for checking if a property exists in an object or a Map: 1. `obj.hasOwnProperty('a')` 2. `map.get('a')` (for Maps) 3. `obj.a` (direct property access) Each test case measures the execution speed of these approaches. **Options Compared** The benchmark compares the following options: * Direct property access (`obj.a`) * Property access using `hasOwnProperty()` (`obj.hasOwnProperty('a')`) * Property access using `get()` method (`map.get('a')` for Maps) These options have different performance characteristics, which we'll discuss below. **Pros and Cons** 1. **Direct Property Access (`obj.a`)** * Pros: + Fastest approach (no function call overhead) * Cons: + May throw an error if the property doesn't exist + Not supported for Maps (since they don't have a `hasOwnProperty()` method) 2. **Property Access using `hasOwnProperty()` (`obj.hasOwnProperty('a')`)** * Pros: + Safe (throws an error instead of throwing NaN) * Cons: + Slower than direct property access due to the function call overhead 3. **Property Access using `get()` method (`map.get('a')` for Maps)** * Pros: + Safe (throws an error instead of throwing NaN) * Cons: + Only available for Maps, not objects + Slower than direct property access due to the function call overhead **Library and Special Features** In this benchmark, no special JavaScript features or libraries are used beyond the standard language. **Considerations** When choosing between these approaches, consider the following: * If you need a safe way to check if a property exists without risking errors, use `hasOwnProperty()` or `get()`. * If performance is critical and the property is guaranteed to exist, use direct property access (`obj.a`). Now, let's take a look at some general alternatives to these approaches: Alternatives to Direct Property Access: * **Bracket notation**: Instead of using dot notation (e.g., `obj.a`), you can use bracket notation (e.g., `[a]`) for accessing properties. * **Object.keys()`: You can use the `Object.keys()` method to get an array of property names and then check if the desired property exists. Alternatives to Property Access using `hasOwnProperty()`: * **Using a regex**: You can use a regular expression (e.g., `/a/`) to check if a property exists. * **Using the `in` operator**: Instead of `hasOwnProperty()`, you can use the `in` operator (e.g., `obj in obj`) to check if an object has a specific property. Keep in mind that these alternatives may have different performance characteristics and edge cases compared to the approaches tested in this benchmark.
Related benchmarks:
Object spread vs New map
Object spread vs New map with string keys
Object.keys().length vs Map.size
testestets 2
Map vs Object read performance for a 1000 key lookup
Comments
Confirm delete:
Do you really want to delete benchmark?