Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash compact vs native
(version: 0)
Comparing performance of:
_lodash vs native
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.core.js"></script>
Tests:
_lodash
_.compact([0, 1, false, 2, '', 3]);
native
[0, 1, false, 2, '', 3].filter(Boolean)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_lodash
native
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_lodash
11426901.0 Ops/sec
native
8222051.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **What is being tested?** The provided benchmark tests two approaches to compacting an array in JavaScript: 1. Using Lodash (`_lodash`): Specifically, the `_.compact()` function from the Lodash library. 2. Native JavaScript: Without any external libraries, using only built-in JavaScript functions. **Options compared** Two options are being compared: * **Lodash**: A popular utility library for functional programming in JavaScript. The `compact()` function is used to remove all falsy values from an array. * **Native JavaScript**: The native JavaScript way of compacting an array by filtering out falsy values using the `filter()` method. **Pros and Cons** Here's a brief summary of each approach: * **Lodash (`_lodash`)**: + Pros: Easy to use, concise code, and a familiar syntax for those already comfortable with Lodash. + Cons: Adds external dependency (the Lodash library), which may not be desirable in certain contexts. Additionally, the performance might not be as good as native JavaScript. * **Native JavaScript**: + Pros: Faster execution speed since it doesn't add any external dependencies. It's also a more straightforward and intuitive way to filter out falsy values. + Cons: Requires an understanding of the `filter()` method and its behavior with falsy values. The choice between these two approaches depends on your specific needs, personal preference, and performance requirements. **Library usage** In this benchmark, Lodash is used for the `_.compact()` function. The library provides a convenient way to compact arrays while maintaining readability and ease of use. **Special JS feature or syntax** There's no special JavaScript feature or syntax being tested in these two test cases. Both approaches rely on standard JavaScript features like arrays, functions, and conditionals. **Other alternatives** If you prefer not to use Lodash or the `filter()` method, you could also use other libraries like Array.prototype.filter() with a callback function that returns true for desired values and false for falsy ones. Alternatively, you can implement your own custom solution using native JavaScript functions, but this approach might be more prone to errors. For example, using only native JavaScript: ```javascript const array = [0, 1, false, 2, '', 3]; const compactedArray = []; for (let i = 0; i < array.length; i++) { if (array[i]) { compactedArray.push(array[i]); } } ``` Keep in mind that this approach might not be as concise or readable as using the `filter()` method.
Related benchmarks:
trim loadsh vs native trim
isFunction vs typeof function 6
isEmpty vs. vanilla
lodash isFunction vs native
Comments
Confirm delete:
Do you really want to delete benchmark?