Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
filter(_ => _) vs filter(Boolean)
(version: 0)
Comparing performance of:
filter(_ => _) vs filter(Boolean)
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
Script Preparation code:
var elements = [0, 1, false, 2, '', 3];
Tests:
filter(_ => _)
elements.filter(_ => _)
filter(Boolean)
elements.filter(Boolean)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
filter(_ => _)
filter(Boolean)
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/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
filter(_ => _)
53460612.0 Ops/sec
filter(Boolean)
39102912.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases to understand what is being tested. **What is being tested?** The benchmark measures the performance difference between two approaches: filtering an array using an anonymous function (`_ => _`) versus using `Boolean`. The focus is on understanding how JavaScript engines optimize these operations. **Options compared:** 1. **filter(_ => _)**: This approach uses a callback function (an anonymous function that takes one argument) as the predicate to filter elements from the `elements` array. 2. **filter(Boolean)**: This approach uses the built-in `Boolean` value as the predicate, effectively filtering out falsy values (including `false`, `0`, `undefined`, `null`, and empty strings) from the `elements` array. **Pros and cons of each approach:** 1. **filter(_ => _)**: * Pros: + Allows for more flexibility in defining the filter condition. + Can be used with custom predicates. * Cons: + May incur additional overhead due to the anonymous function creation. 2. **filter(Boolean)**: * Pros: + Optimized for filtering out falsy values, which is a common use case. + Typically faster than using an anonymous function. * Cons: + Limited flexibility in defining the filter condition. **Library and purpose:** The benchmark includes a reference to Lodash.js, a popular JavaScript utility library. The `filter` function from Lodash is used as the implementation for both test cases. **Special JS feature or syntax:** There is no explicit mention of any special JavaScript features or syntax being tested in this benchmark. However, it's worth noting that some modern JavaScript engines (like V8) have optimized `Boolean` comparisons to be faster than using an anonymous function as a predicate. **Other alternatives:** If you wanted to test alternative approaches for filtering arrays, here are a few examples: 1. **Array.prototype.filter() without a predicate**: You can use the `filter()` method directly on the array without providing a predicate. 2. **map() and reduce() combination**: Instead of using `filter()`, you could use `map()` to create a new array with filtered elements and then use `reduce()` to flatten the resulting array. Here's an example of how you might test these alternatives: ```json { "Name": "filter(map/reduce)", "Description": null, "Script Preparation Code": "var elements = [0, 1, false, 2, '', 3];", "Html Preparation Code": "<script type=\"text/javascript\" src=\"https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js\"></script>" } [ { "Benchmark Definition": "elements.map(_ => _).reduce((a, b) => a.concat(b))", "Test Name": "filter(map/reduce)" } ] ``` Keep in mind that this would likely introduce additional overhead and may not provide meaningful results for small arrays.
Related benchmarks:
_.compact vs filter(Boolean)
Lodash compact vs native
_.compact vs filter(Boolean) vs filter(element => element)
Lodash.filter vs Lodash.without vs array.filter
Comments
Confirm delete:
Do you really want to delete benchmark?