Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
filter(Boolean) vs. filter(a => a)
(version: 0)
Removing empties
Comparing performance of:
filter(Boolean) vs filter(a => a) vs filter(a => !!a)
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
window.list = []; for (let i = 0; i < 65536; i++) { window.list.push(Math.random() > 0.5 ? 1 : 0); }
Tests:
filter(Boolean)
list.filter(Boolean);
filter(a => a)
list.filter(a => a);
filter(a => !!a)
list.filter(a => !!a)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
filter(Boolean)
filter(a => a)
filter(a => !!a)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
filter(Boolean)
1334.6 Ops/sec
filter(a => a)
1326.4 Ops/sec
filter(a => !!a)
967.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** MeasureThat.net provides a platform for users to create and run JavaScript microbenchmarks. The benchmark in question is designed to test the performance of two different approaches: `filter(Boolean)` vs. `filter(a => a)`. Both methods are used to remove empty elements from an array. **Tested Options** The following options are compared: 1. **`list.filter(Boolean)`**: This method uses the `Boolean()` function to filter out falsey values (including 0, "", [], etc.). It is equivalent to `filter(a => a)` but has some performance benefits. 2. **`list.filter(a => a)`**: This method uses an arrow function to filter out only the elements that are truthy. **Pros and Cons of Each Approach** 1. **`list.filter(Boolean)`**: * Pros: Typically faster than `filter(a => a)` due to the use of `Boolean()` which is optimized for performance. * Cons: May not be as readable or maintainable, especially for complex filtering logic. 2. **`list.filter(a => a)`**: * Pros: More readable and maintainable, as it clearly conveys the intention of filtering out only truthy values. * Cons: May be slower than `filter(Boolean)` due to the overhead of evaluating the arrow function. **Library and Purpose** The `Boolean()` function is a built-in JavaScript function that converts its argument to a boolean value. It is used here to filter out falsey values from the array. **Special JS Feature or Syntax** None mentioned in this benchmark, but it's worth noting that some modern browsers support `BigInt` literals, which allow for arbitrary-precision integers. However, this feature is not relevant to the current benchmark. **Other Alternatives** If you want to explore other filtering methods, here are a few alternatives: * Using `Array.prototype.filter()` with a callback function: `list.filter(function(a) { return !!a; })` * Using `Array.prototype.map()` and then applying the `Array.prototype.filter()` method: `list.map(function(a) { return !!a; }).filter(function(a) { return a; })` Keep in mind that these alternatives may have different performance characteristics compared to the original methods.
Related benchmarks:
Lodash vs Native Filters : BabbleTech 02
filter vs slice - remove first
Filter vs Set (unique elements)
Set from array vs array Filter unique
Remove first element from array - slice vs filter
Comments
Confirm delete:
Do you really want to delete benchmark?