Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
filter vs compact v555
(version: 0)
Comparing performance of:
ops.filter(op => !!op) vs _.compact(ops) vs ops.filter(Boolean)
Created:
7 years ago
by:
Guest
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:
ops.filter(op => !!op)
ops.filter(op => !!op)
_.compact(ops)
_.compact(ops)
ops.filter(Boolean)
ops.filter(Boolean)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
ops.filter(op => !!op)
_.compact(ops)
ops.filter(Boolean)
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its various components. **Benchmark Definition** The benchmark is defined in JSON format, which includes three main parts: 1. **Script Preparation Code**: This code defines an array of values called `ops`, which will be used as input for the benchmark. The array contains a mix of numbers and null values. 2. **Html Preparation Code**: This code links to the Lodash library (version 4.16.0) via a CDN, which is likely necessary for the benchmark to work correctly. 3. **Benchmark Definition JSON**: This section defines three individual test cases: * `ops.filter(op => !!op)` * `_.compact(ops)` * `ops.filter(Boolean)` **Test Cases** Each test case measures the performance of a different approach: 1. **`ops.filter(op => !!op)`**: This test case uses the built-in JavaScript `filter()` method with an arrow function that checks if each element in the `ops` array is truthy (i.e., not null or undefined). The `!!` operator is used to explicitly convert values to booleans. 2. **`_.compact(ops)`**: This test case uses the Lodash library's `compact()` function, which removes all falsy values from an array. 3. **`ops.filter(Boolean)`**: This test case uses the built-in JavaScript `filter()` method with a literal value `Boolean`, which is equivalent to using the `!!` operator. **Pros and Cons** Here's a brief summary of each approach: 1. **Built-in `filter()` method (with `!!` operator)**: * Pros: Fast, widely supported, and doesn't require an external library. * Cons: May not be as efficient as other approaches for large datasets or arrays with many falsy values. 2. **Lodash `compact()` function**: * Pros: Efficiently removes falsy values, can handle large datasets. * Cons: Requires an external library (Lodash) and may have a slight performance overhead due to the additional dependency. 3. **Built-in `filter()` method with `Boolean` literal**: * Pros: Similar to using `!!` operator, but might be slightly faster since it's a direct literal value. * Cons: Less explicit than using `!!`, and some people might prefer the arrow function approach for its readability. **Library Usage** The benchmark uses Lodash library version 4.16.0, which provides the `compact()` function used in one of the test cases. **Special JS Feature/Syntax** None mentioned. **Other Alternatives** If you need to optimize or compare performance, consider the following alternatives: * Use a different data structure (e.g., `Set` instead of arrays) for filtering. * Implement your own custom filter function using bitwise operators or other techniques. * Compare with alternative libraries like underscore.js, Ramda, or others. Keep in mind that these alternatives might have their own pros and cons, and the benchmark may need to be adjusted accordingly.
Related benchmarks:
filter vs compact v4444
lodash compact vs es6 compact/filter
lodash compact vs native filter
lodash compact vs native filter vs lodash filter
Comments
Confirm delete:
Do you really want to delete benchmark?