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; for (let i = 0; i < window.array.length; i = i + 1) { if (window.array[i] === 3) { x = x + 1; } } 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):
Let's break down what is being tested on the provided JSON. **Benchmark Definition** The benchmark is comparing three different approaches to find the length of an array in JavaScript: 1. **Lodash `filter()` method**: This approach uses the Lodash library to filter the array and then returns the length of the resulting filtered array. 2. **Lodash `sumBy()` method**: Similar to the first approach, but instead of returning the length, it returns the sum of all elements in the array that match a certain condition. 3. **Manual iteration**: This approach uses a for loop to iterate over the array and increment a counter whenever an element matches the condition. **Options being compared** The three approaches are being compared to determine which one is the most efficient. **Pros and Cons of each approach:** 1. **Lodash `filter()` method**: * Pros: concise, readable code; Lodash library provides optimization for common array operations. * Cons: relies on an external library; may have additional overhead due to the extra dependency. 2. **Lodash `sumBy()` method**: * Pros: similar to `filter()`, but can calculate a sum instead of length; also uses Lodash library for optimization. * Cons: same limitations as `filter()`: relies on external library, may have additional overhead. 3. **Manual iteration**: * Pros: no dependencies, lightweight, easy to understand and implement. * Cons: more verbose code; requires manual increment of counter. **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for common tasks such as array manipulation, object merging, and more. The `filter()` and `sumBy()` methods are part of the Lodash library's array functions. **Special JS feature or syntax: None mentioned** There are no special JavaScript features or syntax used in this benchmark. **Other alternatives** If not using Lodash, alternative implementations for the `filter()` and `sumBy()` methods could be: * Using native JavaScript methods such as `Array.prototype.filter()` and `Array.prototype.reduce()` * Implementing a custom loop with manual iteration * Using other library functions from other libraries (e.g., jQuery) However, these alternatives may not provide the same level of optimization or conciseness as Lodash's implementation. **Benchmark preparation code** The script preparation code includes an array `window.array` that is used in all three test cases. The Html Preparation Code includes a script tag that loads the Lodash library. **Latest benchmark result** The latest benchmark results show that: * Firefox 78 performs best for both `Filter length` and `Sum` tests, with execution rates of around 2000-1300 executions per second. * Manual iteration is the slowest option, with an execution rate of around 5000 executions per second. These results suggest that Lodash's implementation provides a significant performance boost over manual iteration, while also being more concise and readable.
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?