Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.filter vs lodash.compact
(version: 0)
Comparing performance of:
lodash compact vs Native filter
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/lodash/4.17.15/lodash.min.js"></script>
Script Preparation code:
var arr = [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,'',undefined,0,NaN];
Tests:
lodash compact
_.compact(arr)
Native filter
arr.filter(item => !!item)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash compact
Native 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
lodash compact
4552464.5 Ops/sec
Native filter
5462472.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **What is being tested?** The provided JSON represents two test cases: 1. `Array.filter vs lodash.compact`: This benchmark compares the performance of two approaches to filter out null values from an array: * **Native JavaScript Filter**: The first test case uses the built-in `filter()` method on a JavaScript array, specifically using the expression `item => !!item` to check if each element is truthy (i.e., not null, undefined, or NaN). * **Lodash Compact Function**: The second test case uses the Lodash library's `compact()` function to achieve the same result. 2. `Array.filter vs lodash.compact`: This benchmark measures the performance of the two approaches on a predefined array. **Options being compared** The two options being compared are: 1. Native JavaScript Filter (`arr.filter(item => !!item)`) 2. Lodash Compact Function (imported via `<script src="https://cdn.jsdelivr.net/lodash/4.17.15/lodash.min.js"></script>`) **Pros and Cons of each approach:** 1. **Native JavaScript Filter** * Pros: + Efficient, since it uses the built-in `filter()` method, which is optimized for performance. + Low memory overhead, as it only creates a new array with filtered elements. * Cons: + May have slower startup times due to the overhead of importing the Lodash library. 2. **Lodash Compact Function** * Pros: + Can be more readable and concise, especially for developers familiar with functional programming concepts. + Often provides additional features and utilities beyond basic filtering. * Cons: + May incur a higher memory overhead due to the need to import an external library. + Slower execution times compared to native JavaScript filters. **Library: Lodash** Lodash is a popular JavaScript utility library that provides various functions for tasks like filtering, mapping, reducing, and more. The `compact()` function in particular helps remove null or undefined values from arrays while preserving the rest of the elements. In this benchmark, Lodash is used to compare its performance against native JavaScript filtering. **Special JS feature: None mentioned** There are no special JavaScript features or syntaxes being tested in this benchmark. **Other alternatives** For comparison, other approaches could include: * Using a third-party library like `underscore` instead of Lodash * Implementing the filtering logic from scratch without using any external libraries * Comparing performance with different JavaScript engines (e.g., V8 vs. SpiderMonkey) However, these alternatives are not explicitly represented in the provided JSON. Keep in mind that this benchmark primarily focuses on comparing the performance of native JavaScript filtering versus Lodash's `compact()` function.
Related benchmarks:
Array.prototype.filter vs Lodash filter
Array.prototype.filter vs Lodash.without
Lodash.filter vs Lodash.without
Lodash.filter vs Lodash.without vs array.filter
Comments
Confirm delete:
Do you really want to delete benchmark?