Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
_.compact vs filter(Boolean) vs filter(element => element)
(version: 0)
Comparing performance of:
_.compact vs filter(Boolean) vs filter(element => element)
Created:
4 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:
_.compact
_.compact(elements);
filter(Boolean)
elements.filter(Boolean);
filter(element => element)
elements.filter(element => element);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
_.compact
filter(Boolean)
filter(element => element)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36
Browser/OS:
Chrome 144 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.compact
46472636.0 Ops/sec
filter(Boolean)
39133880.0 Ops/sec
filter(element => element)
49305484.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! The provided JSON represents a benchmarking test on MeasureThat.net, which compares the performance of three different approaches: `_.compact` from Lodash, `filter(Boolean)`, and `filter(element => element)`. **What is being tested?** In this benchmark, we're testing the performance of each approach on an array of elements (`elements`). The goal is to measure how quickly each function can iterate over the array and return a new array with only the "truthy" values (i.e., non-zero numbers, non-empty strings). **Options compared:** 1. `_.compact(elements)`: This function returns a new array containing all elements of the original array except zero, empty strings, `null`, and `undefined`. 2. `elements.filter(Boolean)`: This function uses the `filter()` method to create a new array containing only the values for which `Boolean` is true. 3. `elements.filter(element => element)`: This function uses an arrow function to filter the elements of the original array, returning only the elements that are truthy. **Pros and Cons:** 1. `_.compact(elements)`: * Pros: Specifically designed for this use case, can be more efficient due to its optimized implementation. * Cons: Might not work as expected if the input array contains unexpected values (e.g., NaNs). 2. `elements.filter(Boolean)`: * Pros: Simple and readable, works well with most inputs. * Cons: Can be slower than `_.compact(elements)` due to the overhead of calling `Boolean()`. 3. `elements.filter(element => element)`: * Pros: Allows for a more flexible filtering approach, can be used in other contexts where truthiness is important. * Cons: Might require additional checks or assumptions about the input data. **Library and purpose:** The Lodash library provides the `_compact` function, which is designed to provide a concise and efficient way to remove falsey values from an array. It's a popular utility library that offers various functions for common programming tasks. **Special JS feature/syntax:** None mentioned in this benchmark, but it's worth noting that some other features like `let`, `const`, or `var` declarations can affect the performance of the test cases. **Other alternatives:** If you need to filter an array of values based on truthiness, you might consider using a simple loop instead of functional programming approaches: ```javascript function compactArray(arr) { let result = []; for (let i = 0; i < arr.length; i++) { if (arr[i] !== 0 && arr[i] !== '' && arr[i] !== null && arr[i] !== undefined) { result.push(arr[i]); } } return result; } ``` Alternatively, you can use `Array.prototype.filter()` or `Array.prototype.reduce()` to achieve the same result. In summary, this benchmark highlights the importance of choosing the right approach for a specific problem. By understanding the trade-offs between different methods, developers can write more efficient and effective code.
Related benchmarks:
_.compact vs array.filter
_.compact vs filter(Boolean)
Lodash compact vs native
filter(_ => _) vs filter(Boolean)
Comments
Confirm delete:
Do you really want to delete benchmark?