Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash dropWhile or reduce-based trim-from-both-ends
(version: 0)
A function to trim the start and end of an array, based on a filter function.
Comparing performance of:
lodash vs reduce
Created:
2 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:
function trimArrayReduce(array, filter) { return array.reduce((p, c) => p.length > 0 ? [...p, c] : filter(c) ? [] : [c], []) .reduceRight((p, c) => p.length > 0 ? [c, ...p] : filter(c) ? [] : [c], []); } function trimArrayLodash(array, filter) { return _.dropRightWhile(_.dropWhile(a, filter), filter) } a = [0, 0, 0, 0, 0, 0, 1, 2, 5,6,7,8,6,4,3,5,7,8, 3, 0, 0, 0, 6,5,3,3,6,7,23,5,7,2,2,3, 8, 9, 4, 0, 0, 0, 0];
Tests:
lodash
b = trimArrayLodash(a, (v) => v === 0);
reduce
b = trimArrayReduce(a, (v) => v === 0);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash
reduce
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 break down what is tested in the provided JSON, and I'll explain the options compared, pros and cons of each approach, and other considerations. **Benchmark Overview** The benchmark measures two different approaches to trimming the start and end of an array based on a filter function: using Lodash's `dropRightWhile` or reducing the array twice (first with `dropWhile`, then with `reduceRight`). **Library: Lodash** Lodash is a popular JavaScript utility library that provides a collection of functional programming helpers, including filtering and manipulation functions like `dropRightWhile`. The library is included in the benchmark by loading it via a script tag in the HTML preparation code. The purpose of using Lodash's `dropRightWhile` is to demonstrate its performance compared to a custom implementation. By using a well-tested and widely-used library, the benchmark provides an unbiased comparison of the two approaches. **Approach 1: Lodash (`trimArrayLodash` function)** This approach uses Lodash's `dropRightWhile` function to remove elements from the end of the array until the filter condition is no longer met. The `dropRightWhile` function takes a predicate (in this case, the filtering function `(v) => v === 0`) and returns an iterator that yields values until the predicate is false. Pros: * Using a well-tested and widely-used library like Lodash can provide a more accurate representation of real-world performance. * The benchmark can focus on the specific use case (filtering and trimming) rather than implementing it from scratch. Cons: * The performance might be affected by other factors, such as the overhead of loading and executing the Lodash library. * Depending on the version of Lodash used, there might be variations in implementation that affect performance. **Approach 2: Custom Reduction (`trimArrayReduce` function)** This approach uses a custom reduction function to remove elements from the start and end of the array. The `trimArrayReduce` function takes two arguments: the input array `array` and the filtering function `filter`. The function first reduces the array with `dropWhile`, returning an iterator that yields values until the filter condition is met. Then, it further reduces the remaining elements with `reduceRight`, removing any empty arrays that would be created. Pros: * The custom implementation allows for direct control over the algorithm and optimization of specific parts. * This approach can provide insights into how different aspects of the reduction process affect performance. Cons: * Implementing a custom solution requires more development effort and might introduce unnecessary complexity. * Without proper optimization, this approach might not be as efficient as using a well-tested library like Lodash. **Other Considerations** 1. **Special JS features or syntax**: Neither test case uses any special JavaScript features or syntax that would affect the performance comparison. 2. **Alternative approaches**: Other possible approaches for trimming an array based on a filter function include: * Using `filter` and `map` together to create a new array with the desired elements. * Implementing a custom algorithm using bitwise operators (e.g., masking or shifting). * Utilizing WebAssembly or other low-level technologies for performance-critical code. Keep in mind that these alternative approaches would likely result in different benchmark results, and it's essential to consider the trade-offs when choosing an implementation strategy.
Related benchmarks:
remove by splice vs filter array v4
comparing Array.from copy and then splice with filter method
Array.prototype.filter vs Lodash filter removing item from array
dropWhile comparison
Comments
Confirm delete:
Do you really want to delete benchmark?