Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Filter Array by Array vs Filter Array by Map + new Map
(version: 0)
Comparing performance of:
Filter by Array vs new Map + Filter by Map
Created:
2 years ago
by:
Guest
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}`)
Tests:
Filter by Array
urlList.filter(url => !denyListArray.some(u => u === url))
new Map + Filter by Map
var denyListMap = new Map(denyListArray.map(u => [u, null])) 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
new Map + 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
2146.7 Ops/sec
new Map + Filter by Map
73520.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested, compared options, pros and cons, and other considerations. **What is being tested?** The benchmark compares two approaches to filter an array: using the `filter()` method with an array as the second argument (`Filter by Array`) and using a new Map with the same elements as the second argument (`new Map + Filter by Map`). The goal is to determine which approach is faster. **Options compared:** 1. **Filter by Array**: Uses the `filter()` method with an array as the second argument. 2. **new Map + Filter by Map**: Creates a new Map with the same elements as the filter condition and uses it to filter the original array. **Pros and Cons:** * **Filter by Array**: + Pros: - Simple and concise syntax - Fast lookup times since arrays are optimized for this operation + Cons: - May not be as efficient for large datasets due to the overhead of creating a new array - Can be slower than using a Map if the dataset is very large * **new Map + Filter by Map**: + Pros: - Can be faster for large datasets since Maps are optimized for key-value pairs - Reduces memory allocation and garbage collection overhead + Cons: - More verbose syntax due to the creation of a new Map object - May have slower lookup times compared to array-based filtering **Library/Technology:** * **Map**: A JavaScript object that stores key-value pairs. In this benchmark, a new Map is created with the elements of `denyListArray` as keys and `null` as values. **Special JS feature/syntax:** None mentioned in this benchmark. However, it's worth noting that some newer JavaScript features, such as `Array.prototype.map()` or arrow functions (`=>`), are not included in older browsers. If you're targeting older browsers, you may need to use polyfills or workarounds for these features. **Other alternatives:** 1. **Using a Set**: Similar to using a Map, but with a faster lookup time since Sets are optimized for membership testing. 2. **Using `indexOf()` or `includes()` methods**: Instead of filtering the array directly, you can use the `indexOf()` method to find the index of each element in the `denyListArray` and then check if it's within the bounds of the original array. Alternatively, using the `includes()` method with a generator expression can achieve similar results. 3. **Using a custom implementation**: Depending on your specific requirements, you might consider implementing a custom filtering algorithm that takes into account the characteristics of your dataset. Keep in mind that these alternatives may not be as efficient or concise as the original approaches used in this benchmark.
Related benchmarks:
flatMap() vs filter().map() - arrays
Filter and Map vs Reduce
Filter Array by Array vs Filter Array by Map
Reduce Push vs. flatMap vs 123
Comments
Confirm delete:
Do you really want to delete benchmark?