Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
List vs Set
(version: 0)
Comparing performance of:
filter and includes vs set
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data1 = [ ...Array.from(Array(10000).keys()) ]; var data2 = [ ...Array.from(Array(1000).keys()) ];
Tests:
filter and includes
const filteredArray = data1.filter(value => data2.includes(value));
set
const filteredArray = data1.filter(Set.prototype.has, new Set(data2));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
filter and includes
set
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
gemma2:9b
, generated one year ago):
This benchmark tests the performance of filtering an array using two different methods: **Method 1: `filter` with `includes`** - The code `data1.filter(value => data2.includes(value))` uses the built-in `filter` method on the `data1` array. - For each element in `data1`, it calls `includes` on `data2` to check if the element exists in `data2`. If it does, the element is kept in the resulting filtered array. **Method 2: `filter` with a `Set`** - The code `data1.filter(Set.prototype.has, new Set(data2))` uses the `filter` method again but passes two arguments: - `Set.prototype.has`: This refers to the `has` method of the `Set` object, which efficiently checks if a value is present in the set. - `new Set(data2)`: A new `Set` object is created from the `data2` array. Sets are optimized for checking membership (using `has`). **Pros and Cons:** * **Method 1 (`filter` with `includes`)**: - **Con:** The `includes` method iterates through the entire `data2` array every time it's called, which can be slow for large arrays. This leads to O(n) complexity for filtering where n is the size of data2. * **Method 2 (`filter` with `Set`)**: - **Pro:** Using a `Set` offers near constant time complexity (O(1)) for membership checking using `has`. This makes filtering much faster, especially for larger arrays. **Other Considerations:** * **Data Size:** The performance difference between the two methods will be most noticeable with larger `data2` arrays. * **Memory Usage:** Creating a `Set` consumes additional memory, which might be a factor if memory is a significant concern. **Alternatives:** There are other ways to achieve filtering: * **Array Methods (`forEach`, `some`)**: You could manually iterate through `data1` and check for membership in `data2` using a loop or methods like `forEach` and `some`. However, these often lack the performance optimizations of built-in array methods. * **External Libraries:** Some libraries (e.g., Lodash) provide efficient filtering functions with different options and behaviors.
Related benchmarks:
Object.fromEntries vs create temp object
Array from() vs Map.keys() vs Map.values() vs spread
set vs array iteration many
array[0] vs array.at(0) on 100000 elements
Object.fromEntries vs Map
Comments
Confirm delete:
Do you really want to delete benchmark?