Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
red vs douFilt
(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 < 12345678; i++) { arr[i] = i; }
Tests:
reduce
const sumReduce = arr.reduce((acc, item) => { item % 2 === 0 ? acc['q'].push(item) : acc['v'].push(item) return acc; }, {q: [], v: []});
filter
const q = arr.filter(item => item % 2 === 0); const v = arr.filter(item => item % 2 !== 0);
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):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The provided JSON represents two microbenchmarks: `reduce` and `filter`. The benchmarks measure the performance of JavaScript functions that manipulate arrays. **Script Preparation Code** The script preparation code is a simple loop that generates an array of 12,345,678 elements, where each element is its index in the array. This code sets up the input data for the benchmark tests. ```javascript var arr = []; for (var i = 0; i < 12345678; i++) { arr[i] = i; } ``` **Html Preparation Code** The HTML preparation code is empty, indicating that no additional setup or rendering is required for this benchmark. **Benchmark Tests** There are two individual test cases: 1. **`reduce`**: This test measures the performance of the `reduce()` method, which applies a reduction function to each element in the array and returns a single value. ```javascript const sumReduce = arr.reduce((acc, item) => { if (item % 2 === 0) { acc.q.push(item); } else { acc.v.push(item); } return acc; }, { q: [], v: [] }); ``` This test creates an object with two properties: `q` and `v`, which accumulate the even and odd numbers, respectively. 2. **`filter`**: This test measures the performance of the `filter()` method, which creates a new array containing only the elements that pass the test implemented by the provided function. ```javascript const q = arr.filter(item => item % 2 === 0); const v = arr.filter(item => item % 2 !== 0); ``` This test separates the even and odd numbers into two separate arrays. **Library Usage** Both tests use built-in JavaScript functions: `reduce()` and `filter()`. * `reduce()`: This function applies a reduction function to each element in the array, accumulating a value. The provided code uses a custom reduction function that categorizes numbers as even or odd. * `filter()`: This function creates a new array containing only the elements for which the provided function returns true. Both tests use this function to separate even and odd numbers. **Special JavaScript Features** There are no special JavaScript features or syntax used in these benchmarks. **Other Alternatives** Other alternatives for similar benchmarking purposes include: * Using built-in functions like `map()`, `forEach()`, or creating custom loops. * Utilizing external libraries like Lodash (which provides a `reduce()` and `filter()` implementation). * Leveraging other array manipulation methods, such as `every()`, `some()`, or using bitwise operators. Note that the choice of alternative depends on the specific requirements and performance characteristics being tested.
Related benchmarks:
Reading array length inside vs outside for loop
empty an array in JavaScript - splice vs setting length. 444
empty an array in JavaScript - splice vs setting length. 444 333
arr.at(index) vs arr[index]
Array.push(x) vs array[n]=x
Comments
Confirm delete:
Do you really want to delete benchmark?