Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash filter length vs sumby
(version: 0)
Comparing performance of:
Filter length vs Sum vs Manual
Created:
9 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/lodash/4.16.2/lodash.min.js"></script>
Script Preparation code:
window.array = [1, 4, 3, 4, 2, 2, 1, 5, 3, 5, 1, 4, 3, 4, 2, 2, 1, 5, 3, 5, 1, 4, 3, 4, 2, 2, 1, 5, 3, 5, 1, 4, 3, 4, 2, 2, 1, 5, 3, 5, 1, 4, 3, 4, 2, 2, 1, 5, 3, 5];
Tests:
Filter length
const x = _.filter(window.array, i => i === 3).length; return x;
Sum
const x = _.sumBy(window.array, i => i === 3 ? 1 : 0); return x;
Manual
let x = 0; let array = window.array; let len = array.length; for (let i = 0; i < len; i++) { if (array[i] === 3) { x++; } } return x;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Filter length
Sum
Manual
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'll break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark compares the performance of three approaches to count the number of occurrences of the value `3` in an array: using Lodash's `filter()` function with the length property (`Filter length`), using Lodash's `sumBy()` function with a custom callback (`Sum`), and manually iterating through the array (`Manual`). **Lodash Library** The benchmark uses Lodash, a popular JavaScript utility library. In this case, it's used for two functions: 1. `_.filter()`: filters an array to return only elements that pass the test implemented by the provided function. 2. `_.sumBy()`: calculates the sum of each element in the array using the provided function. **Options Compared** The benchmark compares the performance of three options: 1. **Filter length**: uses Lodash's `filter()` function with the length property to count the number of occurrences of `3`. 2. **Sum**: uses Lodash's `sumBy()` function with a custom callback to count the number of occurrences of `3` (by returning 1 for each occurrence and 0 otherwise). 3. **Manual**: manually iterates through the array using a `for` loop to count the number of occurrences of `3`. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: 1. **Filter length**: * Pros: concise, easy to understand. * Cons: relies on Lodash's internal implementation, which might not be optimized for performance. 2. **Sum**: * Pros: decouples counting from filtering, can be more efficient if the array is large and the filter predicate is complex. * Cons: requires an additional library (Lodash), and the callback function may introduce overhead. 3. **Manual**: * Pros: no external dependencies, straightforward implementation. * Cons: verbose, error-prone, and might not be optimized for performance. **Device Platform and OS** The benchmark is run on a Windows desktop with Chrome 118 browser. The results are executed per second, indicating the number of times each approach is executed in one second. **Other Considerations** * The benchmark uses a relatively small array (`window.array`) to keep the dataset manageable. * The `Manual` approach assumes that the array elements are integers and that `3` will be present multiple times. If this assumption is not valid, the results may not be accurate. * The benchmark does not account for potential side effects or optimizations in the Lodash library's internal implementation. **Alternatives** If you want to test similar benchmarks without using Lodash, you can explore other JavaScript libraries or built-in functions that provide filtering and summing capabilities. Some alternatives include: * `Array.prototype.filter()` (native JavaScript function) * `Array.prototype.every()` and `Array.prototype.some()` (native JavaScript functions) * `Array.prototype.reduce()` with a custom callback (native JavaScript function)
Related benchmarks:
Lodash filter length vs sumby
Lodash filter length vs sumby
Lodash filter length vs sumby
Lodash filter length vs sumby 20211125
Comments
Confirm delete:
Do you really want to delete benchmark?