Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array vs object filter
(version: 0)
Comparing performance of:
filter by array vs filter by object
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var supported = ['AValue', 'BValue'] var supportedObj = { 'AValue': true, 'BValue': true } var data = [] for (let i = 0; i < 300; i++) { data.push(`AValue${i}`) data.push(`BValue${i}`) data.push(`CValue${i}`) data.push(`DValue${i}`) }
Tests:
filter by array
const filtered = data.filter(item => supported.includes(item))
filter by object
const filtered = data.filter(item => supportedObj[item])
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 object
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.2:3b
, generated one year ago):
I'll break down the benchmark and its options for you. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark that compares two approaches to filtering an array: 1. `Array.prototype.filter()` method using an array (`supported`) as a predicate function. 2. `Array.prototype.filter()` method using an object (`supportedObj`) where keys are the values from the array and values are booleans indicating membership. **Options Compared** The benchmark tests the performance of these two approaches on a dataset with 300 elements: * `filter by array`: uses the `includes` method to check if each element is in the `supported` array. * `filter by object`: uses the `supportedObj` object to look up each element in the data as a key and retrieve its corresponding value. **Pros and Cons of Each Approach** 1. **Filter by Array (`supports`)**: * Pros: Simple, efficient, and widely supported. * Cons: May be slower than the object-based approach if `supported` is large or if many elements are not present in it. 2. **Filter by Object (`supportedObj`)**: * Pros: Can be faster for large datasets with a small number of distinct values, as it avoids repeated `includes` calls. * Cons: Requires additional memory to store the `supportedObj`, and may be slower if the object is large or if many elements are not present in it. **Library and Purpose** In this benchmark, no external library is used. However, some features of JavaScript are utilized: * The `includes` method (introduced in ECMAScript 2015) for checking membership in an array. * Template literals (`${i}`) for generating string values. **Test Case Considerations** When running this benchmark, consider the following factors that might affect results: * Data distribution: If the data is highly skewed or has a small number of distinct values, the object-based approach may perform better. * Array size and complexity: Larger arrays with many elements can impact performance, but in this case, both approaches are tested on the same dataset. * JavaScript engine optimizations: Different engines might optimize `includes` calls differently. **Other Alternatives** For similar filtering scenarios: * Use the `Array.prototype.findIndex()` method to find the index of the first element that passes the test, and then use `slice()` or `arraySplice()` to extract the filtered elements. * Utilize a library like Lodash's `filterBy` function, which provides an efficient and readable way to filter arrays based on a predicate function. Keep in mind that this benchmark focuses specifically on comparing these two approaches using the `includes` method. Depending on your specific use case and requirements, you may want to experiment with other filtering techniques or libraries.
Related benchmarks:
lodash v native filter
Array.prototype.filter vs Lodash filter
reduce vs filter
Array slice vs array filter
JavaScript Array Slice vs Filter
Comments
Confirm delete:
Do you really want to delete benchmark?