Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
filter -> includes vs filter -> Set.has() vs populate Map v2
(version: 2)
Comparing performance of:
filter -> includes vs filter -> Set.has() vs Populate Map
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var books = Array.from({length: 1000}, (_, index) => ({id: index, mensionedIn: [{ids: [index, index + 1, index + 2, index + 3]}]})) var bookMap = new Map(); books.forEach(book => { book.mensionedIn.forEach(mension => { mension.ids.forEach(id => { if (!bookMap.has(id)) { bookMap.set(id, []); } bookMap.get(id).push(book); }); }); });
Tests:
filter -> includes
var getBooksWithIndex_1 = (index) => { return books.filter(book => book.mensionedIn.some(mensioned => mensioned.ids.includes(index))) } getBooksWithIndex_1(4) getBooksWithIndex_1(5)
filter -> Set.has()
const getBooksWithIndex_2 = (index) => { const bookIdSet = new Set([index]) return books.filter(book => book.mensionedIn.some(mensioned => mensioned.ids.some(bookIndex => bookIdSet.has(bookIndex)))) } getBooksWithIndex_2(4) getBooksWithIndex_2(5)
Populate Map
const getBooksWithIndex_3 = (index)=> { return bookMap.get(index); } getBooksWithIndex_3(4) getBooksWithIndex_3(5)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
filter -> includes
filter -> Set.has()
Populate Map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
filter -> includes
34191.1 Ops/sec
filter -> Set.has()
11541.6 Ops/sec
Populate Map
9027578.0 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. **Benchmark Overview** The provided benchmark is designed to compare three approaches for filtering books in an array based on their IDs: 1. `filter -> includes`: uses the `includes()` method to check if an ID is present in an array of IDs. 2. `filter -> Set.has()`: uses a `Set` data structure to store and look up book IDs. 3. `Populate Map`: populates a `Map` data structure with book IDs as keys and arrays of books as values. **Approach 1: `filter -> includes`** This approach uses the `includes()` method to filter books based on their IDs. The `includes()` method checks if an array contains a specific value, in this case, the ID to be searched. Pros: * Simple implementation * Widely supported by most JavaScript engines Cons: * Can be slower than other approaches due to the overhead of searching for IDs in the array. * Not suitable for large datasets or high-performance applications. **Approach 2: `filter -> Set.has()`** This approach uses a `Set` data structure to store and look up book IDs. The idea is to create a set of IDs from the filtered results and then use this set to filter out books that don't have the ID in the set. Pros: * Fast lookups using the `has()` method, which has an average time complexity of O(1). * Efficient for large datasets. Cons: * Requires additional memory to store the `Set` data structure. * Can be slower than other approaches when the filter condition is false. **Approach 3: `Populate Map`** This approach uses a `Map` data structure to store book IDs as keys and arrays of books as values. The idea is to populate the map with book IDs and then use this map to retrieve the corresponding book array for each ID. Pros: * Fast lookups using the `get()` method, which has an average time complexity of O(1). * Can be more efficient than other approaches for large datasets. Cons: * Requires additional memory to store the map data structure. * Can be slower than other approaches when the filter condition is false. **Library/Functionality Used** In this benchmark, the following libraries or functions are used: * `Array.prototype.filter()`: a built-in JavaScript method for filtering arrays. * `Set`: a built-in JavaScript data structure for storing unique values. * `Map`: a built-in JavaScript data structure for storing key-value pairs. **Special JS Feature/Syntax** The benchmark uses the following special JavaScript features/syntax: * Arrow functions (`=>`) and function expressions (e.g., `const getBooksWithIndex_1 = ...`) * Template literals (e.g., `\r\nvar bookMap = new Map();\r\n`) These features are widely supported by modern JavaScript engines, but may not be compatible with older browsers or environments. **Alternatives** Other approaches that could have been used to filter books include: * Using a `for...of` loop or `forEach()` method instead of `Array.prototype.filter()`. * Using a custom implementation for filtering and lookup operations (e.g., using bitwise operations). * Using other data structures, such as binary search trees or hash tables. However, the provided approaches (`filter -> includes`, `filter -> Set.has()`, and `Populate Map`) are common and efficient solutions for this problem.
Related benchmarks:
Test filter and map123
filter vs flatMap
filter vs flatMap v2
flatMap vs filter + map
Flat map + filter vs. Reduce
Comments
Confirm delete:
Do you really want to delete benchmark?