Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Reduce vs Flatmap 10000 items real
(version: 0)
Comparing performance of:
Reduce vs Flatmap
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
let id = 0 function generateId() { id = id + 1 return id } function generateOject(id) { changes = [] for (let i = 0; i < 10; i++) { changes.push({ id, oldValue: i * 10, newValue: i * 20 }) } return { id, name: `Object#${id}`, changes } } window.lookup = {}; for (let i = 0; i < 10000 ; i++) { window.lookup[generateId()] = generateOject(); }
Tests:
Reduce
const uniqNodes = new Set(); const changes = Object.values(window.lookup).reduce((changes, node) => { if (!uniqNodes.has(node)) { uniqNodes.add(node); return changes.concat(node.changes) } return changes }, []);
Flatmap
const uniqNodes = new Set(); Object.values(window.lookup).forEach(node => { if (!uniqNodes.has(node)) { uniqNodes.add(node); } }); const changes = Array.from(uniqNodes).flatMap(node => node.changes)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Reduce
Flatmap
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 benchmark. The provided JSON represents two test cases: "Reduce" and "Flatmap". These test cases are designed to compare the performance of reducing an array in two different ways: using the built-in `reduce()` method or using the `flatMap()` method. **What is being tested?** In both test cases, we have a large array of objects (`window.lookup`) with 10,000 items. The task is to extract a subset of these objects and concatenate their changes arrays into a single array. For "Reduce", the code uses the `reduce()` method to iterate over the array and build up the final result array. For "Flatmap", the code uses the `flatMap()` method, which applies an array method to each element in the array, flattening it into a one-dimensional array. **Options compared:** The two options are: 1. **Reduce**: Uses the built-in `reduce()` method to iterate over the array and build up the final result array. 2. **Flatmap**: Uses the `flatMap()` method to apply an array method (in this case, `Object.values().forEach()`) to each element in the array. **Pros and Cons:** * **Reduce**: Pros: + Built-in method, so it's likely to be implemented efficiently by the browser. + Can take advantage of early returns, which can reduce the number of iterations. Cons: + May require additional memory allocation for the accumulator. + Can be slower if the array is very large due to the overhead of the `reduce()` method. * **Flatmap**: Pros: + Often faster than `reduce()` because it avoids the need for an accumulator and can take advantage of parallel processing. Cons: + May require more memory allocation due to the creation of a new array. + Can be slower if the array is very large due to the overhead of iterating over each element. **Library:** In both test cases, we use the `window.lookup` object, which appears to be a custom data structure. However, in the "Flatmap" test case, we also use the `Object.values()` method, which is a built-in method for accessing the values of an object. **Special JS feature or syntax:** In both test cases, we're using JavaScript's built-in array methods (`reduce()`, `flatMap()`, and `forEach()`). However, it's worth noting that the `flatMap()` method was introduced in ECMAScript 2019 (ES10), so browsers may not support it if they haven't been updated to use a more recent version of the standard. **Other alternatives:** If we wanted to compare the performance of other methods for reducing an array, some alternatives could include: * Using a `for` loop with index variables * Using a `forEach()` method without a callback function (e.g., `Object.values().forEach(() => {})`) * Using a custom implementation using recursion or iteration However, it's worth noting that the built-in `reduce()` and `flatMap()` methods are often optimized for performance and may not have significant alternatives.
Related benchmarks:
Reduce vs flatMap performance
flatMap vs reduce with push testtttteste212312
Flatmap vs reduce with objects
flatMap vs reduce (push)
flatMap vs reduce-push vs flat
Comments
Confirm delete:
Do you really want to delete benchmark?