Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Filter Boolean vs. Custom Function
(version: 0)
Testing the performance of the idiomatic .filter(Boolean) vs. filtering by a custom coercing function.
Comparing performance of:
filter(Boolean) vs filter function
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = Array.from({length: 10000}, () => Math.random() > 0.5 ? null : "foo");
Tests:
filter(Boolean)
const numTruthy = arr.filter(Boolean).length; console.log(numTruthy);
filter function
const numTruthy = arr.filter(x => !!x).length; console.log(numTruthy);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
filter(Boolean)
filter function
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3 Safari/605.1.15
Browser/OS:
Safari 18 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
filter(Boolean)
41445.6 Ops/sec
filter function
40824.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested. **Benchmark Definition:** The benchmark is testing the performance of two approaches: 1. `filter(Boolean)`: This is an idiomatic way to filter out falsy values from an array in JavaScript. The `Boolean` function returns a value that is coerced to a boolean, which is then used as a predicate in the filtering process. 2. Filtering by a custom coercing function: In this approach, a custom function is used to coerce each element of the array to a boolean value before applying the filter. **Options Compared:** The benchmark is comparing the performance of two different approaches: * `filter(Boolean)`: This approach uses the built-in `Boolean` function to coerce falsy values to `false`, which are then filtered out. * Filtering by a custom coercing function: In this approach, a custom function is used to coerce each element of the array to a boolean value before applying the filter. **Pros and Cons of Each Approach:** 1. `filter(Boolean)`: * Pros: + Efficient, as it uses built-in functions that are optimized for performance. + Easy to read and understand. * Cons: + May not be suitable for all use cases, such as filtering arrays with non-boolean values. 2. Filtering by a custom coercing function: * Pros: + More flexible than `filter(Boolean)`, as it allows for arbitrary coercion logic. + Can be useful when dealing with complex data types or edge cases. * Cons: + May have performance overhead due to the custom coercion logic. + Requires more code and may be less readable. **Library/Custom Function:** In this benchmark, no external library is used. However, the `filter()` function itself is a built-in JavaScript method that returns an array of elements that pass the test implemented by the provided function. **Special JS Feature/Syntax:** There are no special JavaScript features or syntaxes mentioned in this benchmark. The focus is on comparing two different approaches to filtering arrays. **Other Considerations:** When writing benchmarks like this, it's essential to consider factors such as: * Array size and distribution * Data type and complexity * Coercion logic (if applicable) * Performance-critical code paths In this case, the benchmark provides a small array of 10,000 elements with an equal split between truthy and falsy values. The custom coercing function is used to coerce each element to a boolean value before applying the filter. **Alternatives:** Other alternatives for filtering arrays in JavaScript might include: * Using `Array.prototype.filter()` with a callback function * Utilizing libraries like Lodash or Ramda for functional programming * Implementing a custom filtering algorithm using loops and conditional statements However, in this benchmark, the focus is on comparing two specific approaches: `filter(Boolean)` and filtering by a custom coercing function.
Related benchmarks:
slice vs filter (10000000)
Filter vs Set (unique elements)
Shorten array -- slice vs filter
slice vs filter for index filtering
Comments
Confirm delete:
Do you really want to delete benchmark?