Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
_.compact vs array.filter
(version: 0)
Comparing performance of:
_.compact vs array.filter
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.11/lodash.core.js"></script>
Script Preparation code:
var array = [undefined, 'cat', false, 434, '', 32.0]
Tests:
_.compact
_.compact(array)
array.filter
array.filter(Boolean)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.compact
array.filter
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/129.0.0.0 Safari/537.36
Browser/OS:
Chrome 129 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.compact
5356424.5 Ops/sec
array.filter
5353179.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**What is being tested?** The benchmark tests the performance of two approaches to remove `undefined` values from an array: the `_.compact()` function from the Lodash library, and the `array.filter(Boolean)` method. **Options compared** There are two options being compared: 1. **_.compact()**: This function returns a new array with all elements removed that are equal to `undefined`. 2. **Array.prototype.filter(Boolean)**: This method creates a new array with all elements that pass the test implemented by the provided function. **Pros and Cons of each approach** **_.compact()** Pros: * More concise and readable code * Lodash library provides a well-maintained and widely-used implementation Cons: * May have additional overhead due to the use of a separate library * May not be as well-optimized for performance compared to native JavaScript methods **Array.prototype.filter(Boolean)** Pros: * Native JavaScript method, potentially more efficient and lightweight * Does not require an external library Cons: * More verbose code * Requires understanding of the `Boolean` context function in JavaScript **Other considerations** Both approaches can be effective, but the choice between them depends on the specific use case and performance requirements. If readability and convenience are more important than raw performance, _.compact() might be a better choice. However, if every ounce of performance counts, using native JavaScript methods like `filter(Boolean)` could be a better option. **Library explanation** The Lodash library is a popular collection of functional programming utilities in JavaScript. It provides many useful functions for tasks such as array manipulation, object iteration, and more. In this benchmark, the `_.compact()` function is used to remove `undefined` values from an array. **Special JS feature or syntax** No special JavaScript features or syntax are being tested in this benchmark. The code snippets provided use standard JavaScript syntax and methods. **Benchmark preparation code explanation** The script preparation code creates an array with `undefined` values, which will be used as input for both tests: `_.compact()` and `array.filter(Boolean)`.
Related benchmarks:
compact function
Lodash compact
Lodash compact vs native
lodash.compact vs js native
Comments
Confirm delete:
Do you really want to delete benchmark?