Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
~ Hashmap vs Array.Filter 20
(version: 0)
Comparing performance of:
Hashmap vs Array.filter
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const counter = 20 const map = new Map() const array = new Array(counter) window.counter = counter window.map = map window.array = array for (let i = 10000; i < 10000+counter; i++) { map.set(i) array[counter] = i }
Tests:
Hashmap
for (let i = 0; i < 20000; i++) { window.map.has(10019) }
Array.filter
for (let i = 0; i < 20000; i++) { window.array.filter((value) => value === 10019) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Hashmap
Array.filter
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Hashmap
20954.2 Ops/sec
Array.filter
1505.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided benchmark measures the performance difference between using a `Map` data structure and filtering an array in JavaScript to retrieve a specific value. **What is being tested?** In this benchmark, two separate test cases are compared: 1. **Hashmap**: A `Map` object is created with 20 keys (indexed by integers from 0 to 19). Then, for 20,000 iterations, the benchmark attempts to access the key `10019` using the `has()` method. 2. **Array.filter**: An array of 20 elements is created and populated with values. For 20,000 iterations, the benchmark filters this array to retrieve all occurrences of the value `10019`. **Options being compared** The main difference between these two approaches lies in how the specific value (`10019`) is accessed: * **Hashmap**: The `has()` method directly checks if a key exists in the map. * **Array.filter**: The `filter()` method creates a new array with all elements that satisfy the provided condition (in this case, filtering out elements that do not equal `10019`). **Pros and Cons** ### Hashmap (`Map.has()`) Pros: * Direct access to the specific key without iterating over the map. * Can be more efficient for large maps. Cons: * Only returns a boolean value indicating whether the key exists (no return value). * May incur additional overhead for map operations. ### Array.filter Pros: * Returns an array with filtered elements, which can be useful for subsequent processing. * Can be more flexible for filtering based on multiple conditions. Cons: * Creates a new array, which may incur additional memory allocation and copying overhead. * Iterates over the original array to find matching values. **Library: `Map`** The `Map` data structure is a built-in JavaScript object that stores key-value pairs. Its primary purpose is to efficiently store and retrieve values by their keys. In this benchmark, the `has()` method is used to check if a specific key exists in the map. **Special JS feature: None mentioned** No special JavaScript features or syntax are utilized in this benchmark beyond what's inherent to the `Map` and array data structures. **Alternatives** Other alternatives for retrieving a specific value from an array or map include: * Using the `indexOf()` method on arrays, which returns the index of the first occurrence of the specified value. * Implementing custom search algorithms, such as binary search, for large datasets. Keep in mind that each alternative has its own trade-offs and potential performance implications.
Related benchmarks:
~ Hashmap vs Array.Filter
~ Hashmap vs Array.Filter 10
Hashmap vs Array.Filter less
Hashmap vs Array.Filter 10
Comments
Confirm delete:
Do you really want to delete benchmark?