Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Reduce v Filter
(version: 0)
Comparing performance of:
Reduce vs Filter
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var voters = [ { name: "Bob", age: 30, voted: true }, { name: "Jake", age: 32, voted: true }, { name: "Kate", age: 25, voted: false }, { name: "Sam", age: 20, voted: false }, { name: "Phil", age: 21, voted: true }, { name: "Ed", age: 55, voted: true }, { name: "Tami", age: 54, voted: true }, { name: "Mary", age: 31, voted: false }, { name: "Becky", age: 43, voted: false }, { name: "Joey", age: 41, voted: true }, { name: "Jeff", age: 30, voted: true }, { name: "Zack", age: 19, voted: false } ];
Tests:
Reduce
voters.reduce((acc, voter) => { return voter.voted ? acc + 1 : acc; }, 0);
Filter
voters.filter(item => item.voted)
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):
I'll break down the provided benchmark and explain what's being tested, the options being compared, pros and cons of each approach, and other considerations. **Benchmark Overview** The benchmark consists of two test cases: "Reduce" and "Filter". Both tests are designed to measure the performance of JavaScript micro-benchmarks on a specific dataset. The dataset is an array of objects representing voters with their respective votes. **Test Case 1: Reduce** In this test, we're reducing the `voters` array by iterating through each object in the array and adding 1 to the accumulator (`acc`) if the voter's `voted` property is true. The final result is returned as a number. ```javascript const voters = [ // ... (dataset) ]; console.log(voters.reduce((acc, voter) => { return voter.voted ? acc + 1 : acc; }, 0)); ``` **Test Case 2: Filter** In this test, we're filtering the `voters` array to only include objects where the `voted` property is true. ```javascript const voters = [ // ... (dataset) ]; console.log(voters.filter(item => item.voted)); ``` **Options Being Compared** The benchmark compares two options: 1. **Reduce**: Iterates through each object in the array, adding 1 to the accumulator if `voted` is true. 2. **Filter**: Creates a new array with only the objects where `voted` is true. **Pros and Cons of Each Approach:** 1. **Reduce**: * Pros: + Efficient use of memory (no unnecessary copies) + Can be faster for large arrays * Cons: + May require more iterations, potentially slower for smaller arrays 2. **Filter**: * Pros: + Faster for small arrays due to fewer iterations + Easier to read and maintain * Cons: + Creates a new array with multiple copies, potentially slower for large arrays **Library: Lodash** Both test cases use the `lodash` library, which is a popular JavaScript utility library. In this case, it's used for its `reduce()` and `filter()` functions. ```javascript const _ = require('lodash'); ``` The `lodash` library provides various functions for common tasks, making it easier to write efficient and readable code. **Special JS Feature/Syntax: None** Neither of the test cases uses any special JavaScript features or syntax beyond standard ES6+ syntax. No arrow functions, async/await, or other advanced features are employed. **Alternatives** If you wanted to measure the performance of these operations without using `lodash`, you could implement them manually: ```javascript // Reduce const reduce = (array, callback, initialValue) => { let result = initialValue; for (let i = 0; i < array.length; i++) { const item = array[i]; result += callback(item, result, array); } return result; }; // Filter const filter = (array, callback) => { const result = []; for (let i = 0; i < array.length; i++) { if (callback(array[i], i, array)) { result.push(array[i]); } } return result; }; ``` Keep in mind that implementing these functions manually would likely be less efficient and more error-prone than using the `lodash` library. This explanation should provide a clear understanding of what's being tested and compared in the benchmark.
Related benchmarks:
lodash_array_objects
lodash_array_objects_2
splice + spread vs filter to remove one item at given index
cache variabled
Comments
Confirm delete:
Do you really want to delete benchmark?