Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Filter Array by Array vs Filter Array by Map
(version: 0)
Comparing performance of:
Filter by Array vs filter by Map
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var urlList = Array.from(Array(1000)).map((_, i) => `${i}`) var denyListArray = Array.from(Array(200)).map((_, i) => `${i * 3}`) var denyListMap = new Map(denyListArray.map(u => [u, null]))
Tests:
Filter by Array
urlList.filter(url => !denyListArray.some(u => u === url))
filter by Map
urlList.filter(url => !denyListMap.has(url))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Filter by Array
filter by Map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Filter by Array
2130.6 Ops/sec
filter by Map
26271.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON data and explain what's being tested. **Benchmark Definition** The benchmark definition is used to specify the JavaScript code that will be executed during the test. In this case, there are two separate benchmarks defined: 1. `urlList.filter(url => !denyListArray.some(u => u === url))`: This benchmark filters the `urlList` array by checking if each element exists in the `denyListArray`. The `some()` method is used to check if at least one element in the array matches the condition. 2. `urlList.filter(url => !denyListMap.has(url))`: This benchmark filters the `urlList` array by checking if each element exists in the `denyListMap`. The `has()` method is used to check if a key exists in the map. **Options Compared** In this benchmark, two options are being compared: 1. **Filtering an array using the `some()` method**: This approach checks if at least one element in the array matches the condition. It's a simple and straightforward way to filter an array. 2. **Filtering an array using a Map**: The second benchmark uses a Map to store the deny list, and then uses the `has()` method to check if each element exists in the map. This approach is more efficient than the first one because Maps have an average time complexity of O(1) for lookups, whereas arrays have a time complexity of O(n). **Pros and Cons** Here are some pros and cons of each approach: * **Filtering an array using `some()`**: + Pros: Simple to implement, easy to understand. + Cons: Time complexity is O(n), which can be slow for large datasets. * **Filtering an array using a Map**: + Pros: Average time complexity is O(1), making it much faster for large datasets. Also eliminates the need for explicit loops. + Cons: Requires creating a Map, which can add overhead. **Library and Purpose** The `Map` data structure is a built-in JavaScript object that stores key-value pairs. In this benchmark, the `Map` is used to store the deny list, allowing for efficient lookups using the `has()` method. **Special JS Feature or Syntax** There are no special JavaScript features or syntax being used in this benchmark. **Other Alternatives** If you want to filter an array without using the `some()` method or a Map, you could use other approaches such as: * Using the `indexOf()` method and checking if the result is -1. * Using the `includes()` method (which was introduced in ECMAScript 2019). * Using regular expressions. However, these alternatives may have different time complexities or require additional libraries/syntax. Keep in mind that this benchmark is designed to compare the performance of two specific approaches, and the results should be taken as a comparison between those two.
Related benchmarks:
Array.map().filter(Boolean) vs Array.flatMap()
flatMap() vs filter().map() - arrays
Filter Array by Array vs Filter Array by Map + new Map
Reduce Push vs. flatMap vs 123
Comments
Confirm delete:
Do you really want to delete benchmark?