Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash Filter vs Native Filter with Empty Check in Lodash context with other operations
(version: 0)
Comparing performance of:
Lodash Filter vs Native Filter
Created:
4 years ago
by:
Guest
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:
window.items = [ { id: 'a5', a: 1, b: 2 }, { id: 'a3', a: 1, b: 2 }, { id: 'a8', a: 1, b: 2 }, { id: 'b1', a: 1, b: 2 }, { id: 'ac0', a: 1, b: 2 }, { id: 'a7', a: 1, b: 2 }, { id: 'a0', a: 1, b: 2 }, { id: 'a6', a: 1, b: 2 }, { id: 'a5', a: 1, b: 2 }, { id: 'a4', a: 1, b: 2 }, { id: 'a3', a: 1, b: 2 }, { id: 'a2', a: 1, b: 2 } ]; window.products = { a0: { x: 1, y: 1, z: 1, id: 'a0' }, a1: { x: 1, y: 1, z: 1, id: 'a1' }, a2: { x: 1, y: 1, z: 1, id: 'a2' }, a3: { x: 1, y: 1, z: 1, id: 'a3' }, a5: { x: 1, y: 1, z: 1, id: 'a5' }, a7: { x: 1, y: 1, z: 1, id: 'a7' }, a8: { x: 1, y: 1, z: 1, id: 'a8' }, a9: { x: 1, y: 1, z: 1, id: 'a9' }, a10: { x: 1, y: 1, z: 1, id: 'a10' }, a11: { x: 1, y: 1, z: 1, id: 'a11' }, a12: { x: 1, y: 1, z: 1, id: 'a12' }, a13: { x: 1, y: 1, z: 1, id: 'a13' }, a14: { x: 1, y: 1, z: 1, id: 'a14' }, a15: { x: 1, y: 1, z: 1, id: 'a15' }, a16: { x: 1, y: 1, z: 1, id: 'a16' } };
Tests:
Lodash Filter
window.result = _(items) .map(item => item.id) .uniq() .map(id => products[id] || {}) .filter(el => !_.isEmpty(el)) .value(); //console.log('result lodash:', result);
Native Filter
window.result = _(items) .map(item => item.id) .uniq() .map(id => products[id] || {}) .value() .filter(el => !!Object.keys(el).length); //console.log('result native:', result);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash Filter
Native Filter
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):
I'd be happy to help you understand what's being tested in this benchmark. **Overview** The benchmark compares the performance of two approaches: using Lodash (a popular JavaScript utility library) for filtering and transforming an array, versus implementing the same logic natively without any external libraries. **Test Cases** There are two test cases: 1. **Lodash Filter**: This test case uses Lodash to filter the `items` array, which contains objects with multiple properties. The code uses `_` (the Lodash alias) to perform the following operations: * Map each item's ID to a unique value. * Get the corresponding object from the `products` object using the filtered IDs. * Filter out any empty objects from the resulting array. 2. **Native Filter**: This test case implements the same logic without using Lodash. It performs the following operations: * Map each item's ID to a unique value. * Get the corresponding object from the `products` object using the filtered IDs. * Filter out any empty objects from the resulting array (using `!!Object.keys(el).length`). **Library: Lodash** Lodash is a popular JavaScript utility library that provides a set of functions for common tasks, such as: * Array manipulation (`map`, `filter`, `uniq`) * Object manipulation (`get`, `assign`, etc.) * String manipulation (`trim`, `split`, etc.) In this benchmark, Lodash is used to perform the filtering and transformation operations on the `items` array. **Native vs. Library-based Approach** There are pros and cons to each approach: * **Native Approach**: Advantages: + Potential performance benefits due to fewer overheads. + Can be optimized for specific use cases. Disadvantages: + Requires more code and maintenance effort. + May not be as readable or maintainable, especially for complex logic. * **Library-based Approach (Lodash)**: Advantages: + Provides a set of pre-written functions that can be reused across projects. + Often faster and more efficient than implementing similar logic from scratch. Disadvantages: + Requires including an external library, which may increase latency or bundle size. + May not provide the same level of optimization as native code. **Other Alternatives** If you're interested in exploring alternative approaches, consider: * Using other libraries like Underscore.js or Ramda for similar functionality to Lodash. * Implementing custom functions for filtering and transformation operations. * Using a different programming paradigm, such as functional programming with `Array.prototype.filter` and `Object.keys()`. Keep in mind that the choice of approach depends on your specific use case, performance requirements, and personal preferences.
Related benchmarks:
Lodash filter + map vs forEach
Lodash filter vs Native filter
Lodash Filter vs Native Filter with same empty check
lodash isEmpty vs enumerating object keys
Comments
Confirm delete:
Do you really want to delete benchmark?