Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
filter vs compact v2
(version: 0)
Comparing performance of:
lodash filter vs lodash compact vs Native filter vs Native filter no boolean transform vs Native filter Boolean
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 filter no boolean transform
ops.filter(op => op)
Native filter Boolean
ops.filter(Boolean)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
lodash filter
lodash compact
Native filter
Native filter no boolean transform
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 world of JavaScript microbenchmarks on MeasureThat.net. **Overview** The provided JSON represents a benchmark test case that compares the performance of different approaches to filter an array of numbers in JavaScript. The test consists of four individual test cases: 1. **Lodash filter**: Uses the `_filter` method from the Lodash library. 2. **Lodash compact**: Uses the `_compact` method from the Lodash library. 3. **Native filter (no boolean transform)**: A vanilla JavaScript implementation that uses a callback function to filter the array, without applying any transformation to the `Boolean` primitive. 4. **Native filter (with Boolean transformation)**: A vanilla JavaScript implementation that uses the `Boolean` primitive as a transformation function. **What is being tested?** The test case measures the execution time of each approach when filtering an array of numbers using different criteria: * In `_.filter`, the callback function `op => !!op` filters out null values. * In `_.compact`, no filter is applied, and all non-zero values are returned. * In the Native filter (no boolean transform), the callback function `op => op` simply checks if each value is truthy or not. This means that only non-null values will be included in the filtered array. * In the Native filter (with Boolean transformation), the callback function `op => Boolean(op)` transforms all values to their boolean equivalent, effectively filtering out null values. **Options compared** The test case compares the performance of four different approaches: 1. Lodash's `_filter` method 2. Lodash's `_compact` method 3. A native JavaScript implementation with a callback function that filters based on `op => op` 4. A native JavaScript implementation with a callback function that applies a Boolean transformation to each value, using `Boolean(op)` **Pros and cons of each approach** Here are some general pros and cons for each approach: 1. **Lodash filter**: * Pros: Convenient and easy-to-use method from a widely-used library. * Cons: May have performance overhead due to the library's functionality. 2. **Lodash compact**: * Pros: Simple and efficient way to remove null values. * Cons: Not designed for filtering with specific conditions; can lead to unexpected behavior if used incorrectly. 3. **Native filter (no boolean transform)**: * Pros: Lightweight and direct approach that leverages the `Boolean` primitive. * Cons: May require additional logic to handle non-boolean values correctly. 4. **Native filter (with Boolean transformation)**: * Pros: Flexible way to apply a boolean transformation, but may incur additional overhead due to function calls. * Cons: Can lead to slower performance compared to the native implementation without transformation. **Library and its purpose** The Lodash library provides utility functions for functional programming in JavaScript. In this test case, `_filter` and `_compact` are two such functions that help simplify common tasks. **Special JS feature or syntax** None of the approaches mentioned above utilize any special JavaScript features or syntax beyond standard JavaScript language constructs (e.g., `Boolean`, `!!`).
Related benchmarks:
lodash compact vs es6 compact/filter
lodash compact vs native filter
filter vs compact w/ no op
lodash compact vs native filter vs lodash filter
Comments
Confirm delete:
Do you really want to delete benchmark?