Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
filter vs compact
(version: 1)
Comparing performance of:
lodash filter vs lodash compact vs Native filter
Created:
9 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/lodash/4.16.0/lodash.min.js"></script>
Script Preparation code:
var ops = [null, 1, 2, 3, 4, 5, null, null, null, 3, 4,5, 6, 34,341,2,2,2,1,212,21212,34,1,3112, null];
Tests:
lodash filter
_.filter(ops, op => !!op)
lodash compact
_.compact(ops)
Native filter
ops.filter(op => !!op)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash filter
lodash compact
Native filter
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36
Browser/OS:
Chrome 139 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash filter
5241556.0 Ops/sec
lodash compact
13064680.0 Ops/sec
Native filter
10399775.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark data and explain what's being tested. **Benchmark Overview** The benchmark compares the performance of two approaches: using the `_.filter()` method from the Lodash library (a popular utility library for JavaScript) versus a native implementation of the `filter()` function without relying on any external libraries. **What's being tested?** 1. **_.filter()**: The first test case uses the `_.filter()` method from Lodash to filter out null values from an array (`ops`). This is compared against: 2. **Native filter**: The second test case directly implements a native implementation of the `filter()` function, which iterates through the array and returns only non-null elements. 3. **Compact**: The third test case uses the `_._compact()` method from Lodash to remove consecutive null values from an array. **Options compared** The three options being tested are: * **_.filter() (Lodash)**: This is a convenient way to filter arrays using a simple and efficient implementation. * **Native filter**: This approach requires manual implementation of the filtering logic, which can be less readable and more error-prone than relying on a well-tested library like Lodash. * **Compact (Lodash)**: This method removes consecutive null values from an array, which is useful for data processing tasks. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **_.filter() (Lodash)**: + Pros: Convenient, efficient, well-tested, and widely supported. + Cons: Adds external dependency on Lodash. * **Native filter**: + Pros: No external dependencies, allows for manual optimization and control. + Cons: Requires manual implementation of the filtering logic, may be less efficient than using a library. * **Compact (Lodash)**: + Pros: Efficiently removes consecutive null values, useful for data processing tasks. + Cons: Adds external dependency on Lodash. **Special JS features or syntax** None are mentioned in this benchmark. All tests rely on standard JavaScript features and syntax. **Other alternatives** If you're interested in exploring other approaches, here are a few alternatives: * **Array.prototype.filter()**: This is the native method for filtering arrays in JavaScript. While it's not as convenient as using Lodash or writing a custom implementation, it can be a good middle ground. * **Map or Set filtering**: If you need to filter data structures like Maps or Sets, you may want to consider using their built-in filtering methods (e.g., `filter()` on Maps). Overall, this benchmark provides a useful comparison of the performance and convenience of using Lodash's filtering methods versus native implementations.
Related benchmarks:
filter vs compact vs flat
lodash compact vs es6 compact/filter
lodash compact vs native filter
filter vs compact w/ no op
lodash compact vs native filter vs lodash filter
Comments
Confirm delete:
Do you really want to delete benchmark?