Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash compact
(version: 0)
Comparing performance of:
_.compact vs filter
Created:
7 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.js"></script>
Script Preparation code:
var array = [undefined, 'cat', false, 434, '', 32.0]
Tests:
_.compact
_.compact(array)
filter
array.filter(Boolean)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.compact
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/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.compact
47722408.0 Ops/sec
filter
36077476.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided JSON represents a JavaScript microbenchmark, where users can create and run tests to measure the performance of different approaches for compacting an array. **Script Preparation Code** The script preparation code defines an empty array `array` with several values, including undefined, strings, booleans, and numbers. This array is used as input for the benchmark. **Html Preparation Code** The HTML preparation code includes a script tag that loads the Lodash library (version 4.17.11) from a CDN. Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks like array manipulation, string formatting, and more. **Benchmark Definition** The benchmark definition specifies two test cases: 1. `_.compact(array)`: This test case measures the performance of the Lodash `compact` function on the prepared input array. 2. `array.filter(Boolean)`: This test case measures the performance of a simple JavaScript approach using the `filter()` method with `Boolean` as the predicate. **Options Compared** The two approaches being compared are: 1. **Lodash `compact` function**: This function removes all falsy values from an array, leaving only truthy values. 2. **Simple `filter(Boolean)` approach**: This approach uses the `filter()` method with a callback function that always returns `true`, effectively filtering out falsy values. **Pros and Cons** **Lodash `compact` function:** Pros: * concise and readable code * efficient implementation optimized for performance Cons: * requires an external library (Lodash) to be loaded, which may add overhead * may not be suitable for small projects or applications that don't need Lodash functionality **Simple `filter(Boolean)` approach:** Pros: * doesn't require any external libraries or dependencies * can be implemented in a single line of code * potentially faster since it's a built-in JavaScript method Cons: * less readable and maintainable code * may not be optimized for performance like the Lodash implementation **Other Considerations** * The benchmark measures the number of executions per second, which is an important metric for performance comparisons. * The test cases use different input arrays to ensure that the results are consistent across different inputs. **Special JS Feature/Syntax (None)** There are no special JavaScript features or syntax used in this benchmark. **Alternative Approaches** Other approaches for compacting an array might include: 1. Using a custom implementation with `forEach()` and `push()` 2. Utilizing a library like Ramda or Underscore.js 3. Leveraging built-in methods like `map()`, `reduce()`, or `every()`
Related benchmarks:
compact function
empty arr
_.compact vs array.filter
Last Lodash Test
Comments
Confirm delete:
Do you really want to delete benchmark?