Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Filter vs reduce objects test 12312412412
(version: 0)
Comparing performance of:
reduce vs filter
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; for (var i = 0; i < 12345; i++) { arr[i] = { name: "test-name" + i, id: i, value: i, metadata: { position: i }, }; } function someFn(i) { return i.metadata.position % 2 === 0; } var sumReduce = 0, sumFilter = 0;
Tests:
reduce
sumReduce = arr.reduce((prevValue, item) => someFn(item) ? ++prevValue : prevValue, 0);
filter
sumFilter = arr.filter(elem => someFn(elem)).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):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Definition** The benchmark is designed to compare two approaches for filtering and summing an array of objects in JavaScript: `reduce` and `filter`. The script preparation code creates an array of 12,345 objects with a fixed structure, each containing `name`, `tid`, `value`, and `metadata` properties. A custom function `someFn` is defined to filter the array based on the `position` property. **Options Compared** Two options are compared: 1. **Reduce**: Uses the `Array.prototype.reduce()` method to sum up only the elements for which `someFn` returns `true`. 2. **Filter**: Uses the `Array.prototype.filter()` method to create a new array containing only the elements for which `someFn` returns `true`, and then sums up the length of this new array. **Pros and Cons** ### Reduce Pros: * More efficient in terms of memory usage, as it avoids creating an intermediate array. * Can be more efficient if the filtering condition is complex or expensive to evaluate. Cons: * May be slower due to the overhead of the `reduce()` method's accumulation step. * Requires a callback function to define the accumulator and termination conditions. ### Filter Pros: * Often faster than `reduce` for simple filtering cases, as it avoids the accumulation step. * More straightforward to implement and understand. Cons: * Creates an intermediate array, which can consume more memory. * May be slower if the filtering condition is complex or expensive to evaluate. **Library** The benchmark uses the `someFn` function, which relies on the following libraries/syntax features: * No external libraries are required, as `someFn` is a custom function defined within the script preparation code. * JavaScript syntax features are used to define and call `someFn`, including arrow functions (`=>`) and the conditional operator (`? :`). **Special JS Feature/Syntax** The benchmark uses several special JavaScript features/syntax: * Arrow functions (`=>`) * Conditional operator (`? :`) * Template literals (e.g., `"test-name" + i`) * Array iteration syntax (e.g., `for (var i = 0; i < 12345; i++)`) These features are used to define the custom function `someFn`, create the array, and iterate over its elements. **Other Alternatives** Other alternatives for filtering and summing arrays in JavaScript include: * Using `Array.prototype.forEach()` with a callback function * Using `Array.prototype.map()` with a callback function * Using a library like Lodash or Underscore.js for more complex filtering and mapping operations However, MeasureThat.net specifically compares the performance of `reduce` and `filter`, so these alternatives are not considered in this benchmark.
Related benchmarks:
Test: reduce vs for
forEach vs reduce vs map vs filter vs for v2292U9I2JIR2J0IEJ02JE0IJ20EJzdDZD
forEach vs reduce vs map vs filter vs for --ali
Filter and Map vs Reduce
filter vs reduce Birynek
Comments
Confirm delete:
Do you really want to delete benchmark?