Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
_.compact vs filter(Boolean)
(version: 0)
Comparing performance of:
_.compact vs filter(Boolean)
Created:
5 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)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.compact
filter(Boolean)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36
Browser/OS:
Chrome 142 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.compact
42094860.0 Ops/sec
filter(Boolean)
36469500.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested. **Overview** The benchmark compares two JavaScript functions: `_.compact` from Lodash library and the `filter(Boolean)` method on an array. The goal is to determine which function performs better in terms of execution speed, measured in executions per second. **Benchmark Definition JSON** The benchmark definition JSON includes: * `Name`: The name of the benchmark, which is " _.compact vs filter(Boolean)". * `Description`: An empty string, indicating no description for this benchmark. * `Script Preparation Code`: A JavaScript code snippet that creates an array `elements` with various values, including zeros, ones, falsy values, and empty strings. This array will be used as input for both functions being tested. * `Html Preparation Code`: A script tag that loads the Lodash library (version 4.17.15) from a CDN. **Individual Test Cases** The benchmark includes two test cases: 1. **_.compact(elements)**: This test case runs the `_.compact` function on the `elements` array, which removes falsey values from the array. 2. **elements.filter(Boolean)**: This test case runs the `filter(Boolean)` method on the `elements` array, which also removes falsy values. **Library and Syntax** The Lodash library is used in this benchmark. `_.` is a convention for accessing functions within the Lodash namespace. The `compact` function is a utility function that returns a new array with all falsey values removed from the original array. The syntax used here is JavaScript, specifically ES5+ syntax. **Pros and Cons of Each Approach** Both approaches achieve similar results by removing falsey values from the input array. However: * **_.compact(elements)**: This approach uses the `_.compact` function from Lodash, which: + Pros: A well-tested and optimized utility function. + Cons: Requires loading an external library (Lodash). * **elements.filter(Boolean)**: This approach uses a built-in method on arrays in JavaScript: + Pros: Built-in functionality eliminates the need for an external library. + Cons: May have slower performance compared to a dedicated utility function. **Device-Specific Considerations** The benchmark is run on Chrome 129 on a Mac OS X 10.15.7 desktop platform, which might be subject to specific limitations or optimizations. The results may vary on different devices or platforms. **Alternative Approaches** If you wanted to write this benchmark yourself, you could implement the two approaches using standard JavaScript: ```javascript // _.compact(elements) function compact(arr) { const result = []; for (const element of arr) { if (element) { result.push(element); } } return result; } // elements.filter(Boolean) function filterBooleans(arr) { return arr.filter((element) => element !== false); } ``` Keep in mind that these implementations may not be as optimized or efficient as the Lodash implementation.
Related benchmarks:
compact function
Lodash compact
_.compact vs array.filter
Lodash compact vs native
Comments
Confirm delete:
Do you really want to delete benchmark?