Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Filter with include vs filter without include 2
(version: 0)
A comparison between a filter using include vs the same filter without include
Comparing performance of:
Filter 100000 items with include vs Filter 100000 items without include
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var types = ['Type1', 'Type2', 'Type3']; var getType = (i) => types[i % 3]; var fillList = (numberOfItems) => { const list = []; for (var i = 0; i < numberOfItems; i++) { list.push({ name: `dataItem${i}`, value: i, type: getType(i) }); } return list; }; var dataItems = fillList(100000); var dataItemsOfType1And2 = dataItems.filter(dataItem => dataItem.type === types[0] || dataItem.type === types[1])
Tests:
Filter 100000 items with include
dataItems.filter(dataItem => !dataItemsOfType1And2.includes(dataItem));
Filter 100000 items without include
dataItems.filter(dataItem => dataItem.type !== types[0] && dataItem.type !== types[1]);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Filter 100000 items with include
Filter 100000 items without include
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):
**Benchmark Overview** The provided JSON represents a JavaScript microbenchmarking test case on the MeasureThat.net website. The benchmark compares two approaches to filtering an array of objects: one using the `includes()` method with an array as an argument (`dataItemsOfType1And2.includes(dataItem)`), and another without using `includes()` (`dataItem.type !== types[0] && dataItem.type !== types[1]`). **Options Compared** The two options being compared are: 1. **With `includes()`:** This approach uses the `includes()` method to check if an element is present in the `dataItemsOfType1And2` array. 2. **Without `includes()`:** This approach uses a simple explicit comparison (`dataItem.type !== types[0] && dataItem.type !== types[1]`) to filter out elements. **Pros and Cons of Each Approach** ### With `includes()` Pros: * More readable and concise code * Less prone to errors, as the `includes()` method is a built-in function Cons: * May be slower than explicit comparisons for large arrays, due to the overhead of the `includes()` method * Can lead to unnecessary array searches if the array is modified while iterating over it ### Without `includes()` Pros: * Can be faster than using `includes()` for large arrays, as it avoids the overhead of a built-in function call * Less prone to errors, as explicit comparisons are generally safer and more predictable Cons: * More verbose code, which can lead to readability issues * Requires careful handling of edge cases, such as empty or modified arrays **Library and Syntax Considerations** The benchmark uses the `includes()` method, which is a built-in JavaScript function. The syntax for using `includes()` is straightforward: `array.includes(element)`. There are no special JS features or syntax used in this benchmark. **Other Alternatives** If you're looking for alternative approaches to filtering arrays, consider the following: 1. **Using `every()`**: Instead of comparing elements individually, you can use the `every()` method to check if all elements in an array meet a certain condition. 2. **Using `some()`**: Similar to `every()`, but returns `true` as soon as at least one element meets the condition. 3. **Implementing your own filtering logic**: Depending on the specific requirements of your use case, you may be able to optimize or customize the filtering logic to achieve better performance. Keep in mind that these alternatives may not always offer significant performance improvements and should be evaluated on a case-by-case basis.
Related benchmarks:
JavaScript spread operator vs Raw
JavaScript spread operator vs Object.assign performance to merge into new object
JavaScript spread operator vs Object.assign performance - non-destructive merge
JavaScript spread operator vs Object.assign performance (Immutable)
Set#has() vs Array#includes()
Comments
Confirm delete:
Do you really want to delete benchmark?