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
llama3.1:latest
, generated one year ago):
Let's dive into the details of this benchmark. **Benchmark Name:** List vs Set **Description:** This benchmark compares the performance of filtering an array using two different methods: `Array.prototype.includes()` and `Set.prototype.has`. **Test Cases:** 1. **filter and includes**: In this test case, we're using the `filter()` method on an array (`data1`) and checking if each element exists in another array (`data2`) using `includes()`. * Pros: + Easy to implement for developers familiar with arrays. + Works well when the subset is small compared to the original array. * Cons: + Inefficient when dealing with large subsets or frequent lookups, as it has a time complexity of O(n). 2. **set**: In this test case, we're using the `filter()` method on an array (`data1`) and checking if each element exists in a set (`new Set(data2)`) using the `has()` method. * Pros: + Provides better performance when dealing with large subsets or frequent lookups, as it has a time complexity of O(1) on average. + Uses less memory than storing an array of unique values. * Cons: + Requires more code and might be less familiar to developers who don't use sets frequently. **Used Library:** None **Special JS Feature or Syntax:** `Set.prototype.has` is a method that checks if a value exists in the set. It's not a special feature, but rather a built-in method of the `Set` object. **Other Alternatives:** * Using a `Map` instead of an array for storing unique values and using the `has()` method. * Implementing a custom data structure like a hash table or a trie for efficient lookup operations. * Utilizing JavaScript's built-in `Array.prototype.some()` method, which can provide better performance than `filter()`. * If dealing with very large datasets, consider using a database or a specialized caching solution. The results show that the "set" approach outperforms the "filter and includes" method on this particular benchmark. This is likely due to the more efficient lookup operation provided by the set data structure.
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?