Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Reduces vs Filter
(version: 0)
Comparing performance of:
reduce vs filter
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; for (var i = 0; i < 2000; i++) { arr[i] = i; }
Tests:
reduce
sumReduce = arr.reduce((lastValue, item) => ({ nb1: item % 2 == 0 ? lastValue.nb1 + 1 : lastValue.nb1, nb2: item % 2 != 0 ? lastValue.nb2 + 1 : lastValue.nb2, }), {nb1:0, nb2:0});
filter
res = { nb1: arr.filter(item => item % 2 == 0).length, nb2: arr.filter(item => item % 2 != 0).length, }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
reduce
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):
**Benchmark Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark compares the performance of two approaches: `reduce` and `filter`. The goal is to measure which approach is faster for reducing an array. **Script Preparation Code** The script preparation code generates a large array with 2000 elements, where each element is its index. This code creates a realistic scenario for measuring the performance of the `reduce` and `filter` methods. ```javascript var arr = []; for (var i = 0; i < 2000; i++) { arr[i] = i; } ``` **Html Preparation Code** The html preparation code is empty, which means that no HTML is generated for this benchmark. ```javascript <html> ... </html> ``` **Individual Test Cases** There are two individual test cases: 1. **`reduce` Test Case** ```javascript sumReduce = arr.reduce((lastValue, item) => ({ nb1: item % 2 == 0 ? lastValue.nb1 + 1 : lastValue.nb1, nb2: item % 2 != 0 ? lastValue(nb2) + 1 : lastValue(nb2) }), {nb1:0, nb2:0}); ``` This test case uses the `reduce` method to iterate over the array and calculate two counts: `nb1` for even numbers and `nb2` for odd numbers. The accumulator object is initialized with `nb1` and `nb2` set to 0. **Library: Lodash** The `lastValue(nb2)` expression uses the `lodash` library, which provides a function called `lastValue`. This function is used to access the previous value in the accumulator object. In this case, it's used to initialize the `nb2` property with a default value of 0. **Library: Lodash** The `lodash` library is used extensively in JavaScript development for functional programming and utility functions. It provides a wide range of functions for tasks such as array manipulation, string processing, and more. **Filter Test Case** ```javascript res = { nb1: arr.filter(item => item % 2 == 0).length, nb2: arr.filter(item => item % 2 != 0).length }; ``` This test case uses the `filter` method to create two arrays: one with even numbers and one with odd numbers. The counts are then calculated by getting the length of these arrays. **Special JS Feature/ Syntax** There is no special JavaScript feature or syntax used in this benchmark. Both tests use standard JavaScript methods (`reduce`, `filter`) and libraries (Lodash). **Other Alternatives** If you want to measure the performance of other array reduction methods, such as `forEach` or `map`, you can add additional test cases. For example, you could create a test case for using `forEach`: ```javascript arr.forEach((item) => { if (item % 2 == 0) { nb1++; } else { nb2++; } }); ``` Or for using `map`: ```javascript const mappedArr = arr.map((item) => item % 2 == 0 ? 1 : 0); const sumMapped = mappedArr.reduce((acc, val) => acc + val, 0); nb1 += sumMapped; nb2 += (2000 - sumMapped); ``` Keep in mind that each approach has its own trade-offs and may not be suitable for all scenarios.
Related benchmarks:
map filter vs reduce
map filter vs reduce concat
Seomthing
Count matches using Array reduce vs Array filter
flatMap vs reduce vs loop filtering performance vs filter
Comments
Confirm delete:
Do you really want to delete benchmark?