Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map + filter + flat vs Reduce222323
(version: 0)
Given an array input object
Comparing performance of:
Map + filter + flat vs Reduce
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var inputs = [{ id: 1, type: 'product', quantity: 5 }, { id: 2, type: 'product', quantity: 2 }, { id: 3, type: 'bundle', quantity: 1, items: [{ id: 4, type: 'product', quantity: 1 }, { id: 5, type: 'product', quantity: 1 }, { id: 6, type: 'product', quantity: 2 }, { id: 8, type: 'unknown', quantity: 2 } ] }, { id: 7, type: 'product', quantity: 1 }, { id: 8, type: 'unknown', quantity: 1 } ] function extractBundleItems(bundle) { return bundle.items.map(item => { return { item_id: item.id, item_type: item.type, quantity: item.quantity } }) }
Tests:
Map + filter + flat
inputs.map((item) => { if (item.type === 'product') { return { item_id: item.id, item_type: item.type, quantity: item.quantity, } } if (item.type === 'bundle') { return extractBundleItems(item) } return null }).filter(Boolean)
Reduce
inputs.reduce((results, item) => { if (item.type === 'product') { results.push({ item_id: item.id, item_type: item.type, quantity: item.quantity, }) } if (item.type === 'bundle') { results.push(...extractBundleItems(item)) } return results }, [],)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Map + filter + flat
Reduce
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'd be happy to explain what's being tested in the provided benchmark and compare different approaches. **Benchmark Overview** The benchmark measures the performance of two JavaScript functions: `map`, `filter`, and `flat` (combined) versus `reduce`. The test inputs an array of objects with varying types (`product`, `bundle`, and unknown). **Approach 1: Map, Filter, and Flat (Combined)** This approach uses a single line of code to extract relevant data from the input array: ```javascript inputs.map((item) => { if (item.type === 'product') { return { item_id: item.id, item_type: item.type, quantity: item.quantity } } if (item.type === 'bundle') { return extractBundleItems(item) } return null }).filter(Boolean) ``` **Pros:** * Simple and concise code * Uses built-in Array methods **Cons:** * May not be as efficient due to the use of `map` and `filter`, which create new arrays **Approach 2: Reduce** This approach uses a single line of code with `reduce` to extract relevant data from the input array: ```javascript inputs.reduce((results, item) => { if (item.type === 'product') { results.push({ item_id: item.id, item_type: item.type, quantity: item.quantity }) } if (item.type === 'bundle') { results.push(...extractBundleItems(item)) } return results }, []) ``` **Pros:** * Can be more efficient than `map` and `filter`, as it avoids creating new arrays **Cons:** * More complex code due to the use of `reduce` **Library Used:** The library used in this benchmark is not explicitly mentioned, but it appears to be a custom function called `extractBundleItems`. This function takes a `bundle` object and returns an array of extracted items. **Special JS Features/Syntax:** None are explicitly mentioned, but note that the use of arrow functions (`=>`) and template literals (`\r\n...${}`) is used in the benchmark code. These features are supported by most modern JavaScript engines. **Alternatives:** Other approaches to achieving this result might include: * Using a single loop with conditional statements (e.g., `for` loops) * Using a library like Lodash, which provides utility functions for array manipulation * Using a different data structure, such as a hash table or graph, to store the input data It's worth noting that the choice of approach ultimately depends on the specific requirements and constraints of the project.
Related benchmarks:
filter.map vs reduce 2
reduce vs filter+map
Filter and Map vs Reduce
flatMap vs filter + map
Flat map + filter vs. Reduce
Comments
Confirm delete:
Do you really want to delete benchmark?