Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash vs filter
(version: 0)
Comparing performance of:
Lodash vs Filter
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; var object = { a: 'a', b: 'b', c: 'c', d: 'd', e: 'e', f: 'f', g: 'g', h: 'h' }; for (var i = 0; i <= 100000; i++) { arr.push(object); }
Tests:
Lodash
arr.map(function (element) { return _.pick( element, 'a', 'b', 'c', 'd', 'e', 'f' ); });
Filter
const pick = (obj, keys) => { return keys .filter((key) => key in obj) .reduce((res, key) => ({ ...res, [key]: obj[key] }), {}); }; arr.map(function (element) { return pick( element, ['a', 'b', 'c', 'd', 'e', 'f'] ); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash
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/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash
12.2 Ops/sec
Filter
13.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its components. **Benchmark Overview** The benchmark compares the performance of two approaches for filtering an object: Lodash's `pick` function and a custom `filter` function using JavaScript's built-in methods. **Options Compared** 1. **Lodash's `pick` function**: A utility function from the popular Lodash library that allows filtering objects by key. 2. **Custom `filter` function**: A simple, vanilla JavaScript implementation that filters an object by keys. **Pros and Cons of Each Approach:** ### Lodash's `pick` Pros: * Easy to use and readable * Well-maintained and widely adopted * Provides a concise way to filter objects Cons: * Additional library dependency (Lodash) * May have a performance overhead due to the function call ### Custom `filter` function Pros: * No additional dependencies or libraries * Intrinsic JavaScript methods make it easy to understand and use * Can be optimized for specific use cases Cons: * May require more boilerplate code * Less readable than Lodash's `pick` **Library:** Lodash is a popular utility library for JavaScript that provides a wide range of functions for common tasks, such as array manipulation, object filtering, and more. The `pick` function is one of its many useful utilities. **Special JS Feature or Syntax:** None mentioned in this benchmark. Both approaches use standard JavaScript features. **Other Considerations:** * Performance: The custom `filter` function might be optimized for performance, as it directly uses built-in methods. * Learning Curve: Lodash's `pick` is generally easier to learn and use for developers without extensive experience with object filtering. * Dependency Management: The benchmark includes Lodash as a dependency, which may not be desirable for all projects. **Alternative Approaches:** 1. **Using JavaScript's spread operator (`{ ... }`)**: This approach can be used instead of the custom `filter` function. It would eliminate the need for the `filter` method and simplify the code. ```javascript const pick = (obj, keys) => { return keys.reduce((res, key) => ({ ...res, [key]: obj[key] }), {}); }; ``` 2. **Using a library like `lodash-es`**: If you prefer to use Lodash but want to avoid the full `lodash` package size, you can consider using `lodash-es`, which is a smaller version of the library that still provides most of its functionality. ```javascript const _ = require('lodash-es'); const pick = _.pick; ``` 3. **Native JavaScript methods (e.g., `Object.fromEntries()` and `Array.prototype.reduce()`)**: Depending on the specific use case, you might be able to optimize the custom `filter` function using native methods that are already optimized for performance. Keep in mind that these alternative approaches may alter the benchmark's focus or simplicity, so it's essential to evaluate their trade-offs based on your specific requirements.
Related benchmarks:
Array.prototype.filter vs Lodash filter
Array.prototype.filter vs Lodash filter
Array.prototype.filter vs Lodash filter Clone Test
Lodash.filter vs Lodash.without
Array.prototype.filter vs Lodash filter for ~10000
Comments
Confirm delete:
Do you really want to delete benchmark?