Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash compact vs native filter
(version: 0)
Comparing performance of:
lodash compact vs Native filter vs Native filter (Boolean)
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/lodash/4.17.21/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, '', '', 'asdf'];
Tests:
lodash compact
_.compact(ops)
Native filter
ops.filter(op => !!op)
Native filter (Boolean)
ops.filter(op => Boolean(op))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash compact
Native filter
Native 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 dive into the explanation of the provided benchmark. **Benchmark Definition** The benchmark measures the performance difference between two approaches to remove null and empty values from an array: 1. `_.compact(ops)` - This is the Lodash `compact` function, which removes all falsey values from an array. 2. `ops.filter(op => !!op)` - This is a native JavaScript filter function that uses the double-nota operator (`!!`) to check if a value is truthy (i.e., not null or undefined). 3. `ops.filter(op => Boolean(op))` - This is another native JavaScript filter function that casts its input to a boolean value, which will remove null and undefined values. **Options Compared** The benchmark compares the performance of these three approaches: * **Lodash `compact`**: A library-based approach that uses a specialized function for filtering out falsey values. * **Native JavaScript `filter` with `!!op`**: An approach that leverages the double-nota operator to check for truthiness in the filter callback. * **Native JavaScript `filter` with `Boolean(op)`**: Another approach that casts its input to a boolean value. **Pros and Cons** Here's a brief overview of each approach: 1. **Lodash `compact`**: * Pros: Optimized for performance, concise code, and well-maintained by the Lodash community. * Cons: Adds an external dependency (the Lodash library) and might be overkill for simple use cases. 2. **Native JavaScript `filter` with `!!op`**: * Pros: No dependencies required, widely supported by modern browsers, and easy to implement. * Cons: Requires the double-nota operator, which can be confusing or less readable for some developers. 3. **Native JavaScript `filter` with `Boolean(op)`**: * Pros: Similar to the previous approach but uses a more traditional boolean casting method. * Cons: Still requires the filter function and explicit conversion to boolean. **Library Used** The benchmark uses Lodash, a popular JavaScript utility library that provides a wide range of functional programming helpers. The `compact` function is one of its most useful ones. **Special JS Features or Syntax** The benchmark uses the double-nota operator (`!!`) in two places, which might be unfamiliar to some developers. This operator is not part of vanilla JavaScript but has become widely accepted and used in modern web development. **Alternatives** If you're looking for alternatives to Lodash `compact` or want to explore native JavaScript solutions, consider the following options: * Use a different library that provides similar functionality, such as Underscore.js. * Implement your own filter function using vanilla JavaScript, leveraging techniques like `Array.prototype.filter()` and conditional statements. Overall, the benchmark helps developers understand the performance trade-offs between using Lodash's optimized functions versus implementing native JavaScript solutions with double-nota operators or traditional boolean casting.
Related benchmarks:
lodash compact vs es6 compact/filter
filter vs compact w/ no op
lodash compact vs native filter vs lodash filter
Array.filter vs lodash.compact
Comments
Confirm delete:
Do you really want to delete benchmark?