Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
filter vs compact vs flat
(version: 0)
Comparing performance of:
lodash filter vs lodash compact vs Native filter vs Native flat
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:
lodash filter
_.filter(ops, op => !!op)
lodash compact
_.compact(ops)
Native filter
ops.filter(op => !!op)
Native flat
ops.flat()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
lodash filter
lodash compact
Native filter
Native flat
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:122.0) Gecko/20100101 Firefox/122.0
Browser/OS:
Firefox 122 on Ubuntu
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash filter
6095557.0 Ops/sec
lodash compact
8445320.0 Ops/sec
Native filter
4264488.0 Ops/sec
Native flat
4444078.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and explain what's being tested, the options compared, pros and cons of each approach, and other considerations. **Benchmark Overview** The benchmark tests the performance of three different approaches to filter an array: 1. Lodash `compact` function 2. Lodash `filter` function 3. Native JavaScript `filter` method Additionally, it tests two native methods for flattening arrays: `flat()` (which is not actually being tested in this benchmark) and a hypothetical "Native filter" method. **Options Compared** The benchmark compares the performance of: * Lodash `compact` vs. Lodash `filter` * Native JavaScript `filter` method vs. Lodash `filter` Each test case has its own set of inputs (the `ops` array in the Benchmark Definition JSON). **Pros and Cons of Each Approach** 1. **Lodash `compact` vs. `filter`** * Pros: + Lodash is a mature, widely-used library with an efficient implementation. + The compact function removes only the falsy values from the array, which might be beneficial in certain scenarios. * Cons: + Lodash introduces additional overhead due to the need for an external library. 2. **Native JavaScript `filter` method** * Pros: + This approach is implemented directly within the browser's engine and doesn't require any external libraries. + It might be optimized for performance, as it's a fundamental method in the language. * Cons: + The native implementation may not be as efficient or well-optimized as Lodash's compact function. **Other Considerations** * **Device Platform and Browser**: The benchmark is run on a specific device (Ubuntu Linux desktop) with a particular browser version (Firefox 122). This might affect the results due to differences in hardware, software configurations, or browser optimizations. * **Operating System**: The benchmark runs on Ubuntu, which may have specific optimizations or configurations that impact performance. **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks like filtering, mapping, and reducing arrays. The `compact` function specifically removes falsy values from an array, while the `filter` function applies a predicate to each element in the array, returning a new array with only the elements that match the predicate. **Special JavaScript Feature or Syntax** None mentioned explicitly, but keep in mind that this benchmark is running on Firefox 122, which may include some experimental features or syntax not widely supported across browsers.
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?