Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
map VS keys VS hasOwnProperty
(version: 0)
Comparing performance of:
map vs keys vs hasOwnProperty
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
map
const myObj = {one:1,two:2,three:3} const myMap = new Map(Object.entries(myObj)) myMap.has("one")
keys
const myObj = {one:1,two:2,three:3} const keys = Object.keys(myObj) keys.includes("one")
hasOwnProperty
const myObj = {one:1,two:2,three:3} Object.prototype.hasOwnProperty.call(myObj, "one")
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
map
keys
hasOwnProperty
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and explain what's being tested, the pros and cons of each approach, and other considerations. **Benchmark Overview** The benchmark compares the performance of three different approaches to check if a key exists in an object: 1. `map` (using the `Map` data structure) 2. `keys` (using the `Object.keys()` method) 3. `hasOwnProperty` (using the `hasOwnProperty()` method on the prototype chain) **Library Usage** The benchmark uses the following libraries: * None explicitly mentioned, but `Object.entries()` is used, which is a part of the ECMAScript standard. **Test Cases** Each test case has a unique definition and purpose: 1. **`map`**: Tests using a `Map` object to store the keys of an object. It creates a new `Map` from the object's entries using `Object.entries()` and then checks if a specific key exists in the map using the `has()` method. 2. **`keys`**: Tests using the `Object.keys()` method to get an array of an object's own enumerable property names, and then checks if a specific key exists in that array using the `includes()` method. 3. **`hasOwnProperty`**: Tests using the `hasOwnProperty()` method on the prototype chain to check if a specific key exists in an object. **Pros and Cons** Here are some pros and cons for each approach: 1. **`map`**: * Pros: Can be faster for large objects, as it uses a hash table to store keys. * Cons: Requires creating a new `Map` object, which can incur overhead. 2. **`keys`**: * Pros: Simple and straightforward, no additional data structure creation required. * Cons: May perform slower due to the array lookup in `includes()`, especially for large objects. 3. **`hasOwnProperty`**: * Pros: Fastest approach, as it only checks the prototype chain, which is optimized by JavaScript engines. * Cons: Can be slower for very large objects or those with many inherited properties. **Other Considerations** * The benchmark uses a Windows-based browser (Chrome 96) on a desktop platform. Other platforms and browsers may have different performance characteristics. * The benchmark executes each test case multiple times per second, which can affect the results due to other factors like caching, garbage collection, or network latency. * The `map` approach requires creating a new `Map` object, which can incur additional memory allocation. **Alternatives** Other alternatives to check if a key exists in an object include: * Using `in`: This operator checks if a property name is present in the object's own properties and its prototype chain. It may be slower than `hasOwnProperty()` but is generally considered more readable. * Implementing a custom lookup function using a hash table or binary search tree. Keep in mind that these alternatives may have different performance characteristics, and the best approach depends on the specific use case and requirements.
Related benchmarks:
Map has vs get
Array from() vs Map.keys()
Array from() vs Map.keys() vs Map.values() vs spread
set.has vs. array.includes vs. map.has
Object.fromEntries vs Map
Comments
Confirm delete:
Do you really want to delete benchmark?