Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash.filter vs js native
(version: 0)
Remove undefined/null from array
Comparing performance of:
lodash.filter vs array filter
Created:
4 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var array = ['a', undefined, 'b', 1, undefined, 2, 3, undefined, undefined]
Tests:
lodash.filter
_.filter(array, data => { return data !== undefined && data !== null });
array filter
array.filter(data => { return data !== undefined && data !== null });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash.filter
array 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/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash.filter
13590289.0 Ops/sec
array filter
18083404.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark. **What is tested:** The provided JSON represents a JavaScript microbenchmark that compares the performance of two approaches to filter out `undefined` and `null` values from an array: 1. **Lodash's `filter()` function**: The first test case uses Lodash, a popular utility library for JavaScript. 2. **Native JavaScript `filter()` function**: The second test case tests the native JavaScript implementation of the `filter()` function. **Options compared:** The benchmark compares two options: A) Using the `filter()` function provided by Lodash (`_.filter()`). B) Implementing a custom filter using only standard JavaScript features (`array.filter()`). **Pros and Cons:** **Lodash's `.filter()` function:** Pros: * Convenience: Lodash provides a pre-built, tested, and optimized function for filtering arrays. * Abstraction: The `filter()` function abstracts away the implementation details, making it easier to write concise code. Cons: * Overhead: Using an external library like Lodash introduces additional overhead due to: + Loading the library + Creating a new scope for the function + Adding potential dependencies and versioning issues **Native JavaScript `filter()` function:** Pros: * Native performance: Implementing a custom filter using native JavaScript features can lead to better performance, as it avoids the overhead of an external library. * Control: You have direct control over the implementation details, allowing for optimization and customization. Cons: * Code complexity: Writing a custom filter requires understanding the underlying array data structure and implementing the logic yourself. * Potential errors: Without proper testing and optimization, your custom implementation might contain errors or performance regressions. **Library (Lodash):** Lodash is a popular utility library for JavaScript that provides a wide range of functions for tasks like filtering arrays. The `filter()` function is just one of many utility functions available in Lodash. Other benefits of using Lodash include: * Convenience: Many developers are already familiar with Lodash and its API. * Test coverage: Lodash has extensive test coverage, ensuring that the implementation is correct and efficient. **Special JS feature or syntax:** There is no mention of any special JavaScript features or syntax in the provided benchmark. The tests only rely on standard JavaScript features and libraries (Lodash). **Other alternatives:** If you're interested in exploring alternative approaches, here are some options: * Use a different utility library like `underscore.js` or `ramda`. * Implement a custom filter using other libraries like `fastify-filter` or `lodash-es`. * Consider using native JavaScript's built-in functions like `Array.prototype.filter()` or `Array.prototype.every()`. Keep in mind that the choice of approach depends on your specific use case, performance requirements, and personal preferences.
Related benchmarks:
Array.prototype.filter vs Lodash filter
Array.prototype.filter vs Lodash.without
Array.prototype.filter vs Lodash filter removing item from array
lodash.compact vs js native
Comments
Confirm delete:
Do you really want to delete benchmark?