Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
map/filter vs reduce
(version: 0)
Comparing performance of:
map/filter vs reduce
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
map/filter
const tableData = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]; const ids = new Array(100) .fill(null) .map((_, i) => { return tableData[i]; }) .filter(id => id !== undefined);
reduce
const tableData = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]; const ids = new Array(100).fill(null).reduce((acc, _, i) => { if(tableData[i]) { acc[i] = tableData[i] || null } return acc; }, []);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
map/filter
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):
Let's break down the benchmark and explain what is being tested. **What is being tested?** The test case measures the performance difference between two approaches: 1. **`map()`** followed by **`filter()`**: This approach creates an array of values from `tableData`, filters out `undefined` values, and returns the resulting array. 2. **`reduce()`**: This approach iterates over `tableData` and accumulates values in an object, where each key is a unique index and the value is the corresponding element from `tableData`. **Options compared** The two approaches have different performance characteristics: * **`map()` followed by `filter()`**: This approach has higher overhead due to the creation of a new array and the filtering step. It's generally faster for large datasets because it avoids the overhead of creating an intermediate array. * **`reduce()`**: This approach is often considered more efficient because it only requires a single pass over the data, making it suitable for smaller datasets. **Pros and Cons** * **`map()` followed by `filter()`**: + Pros: faster for large datasets due to reduced memory allocation and copying overhead. + Cons: may have higher CPU usage due to the creation of an intermediate array. * **`reduce()`**: + Pros: often more efficient, especially for smaller datasets, because it only requires a single pass over the data. + Cons: may not be as scalable as `map() / filter()` due to its reliance on accumulating values in an object. **Library** The test case uses the **`Array.prototype.map()`**, **`Array.prototype.filter()`**, and **`Array.prototype.reduce()`** methods, which are built-in JavaScript array methods. These methods are designed to work efficiently with arrays and are implemented in native code, making them generally faster than equivalent custom implementations. **Special JS feature or syntax** The test case uses a new JavaScript feature: **template literals** (the `\r\n`-prefixed string literal). Template literals are a way to create strings using an expression inside the backticks (` ``), allowing for more readable and flexible string concatenation. This feature is supported in modern JavaScript engines. **Alternatives** Other approaches to achieve similar results could include: * Using `forEach()` or `for` loops with manual iteration and array indexing. * Implementing a custom iterator using `Iterator` or `Generator` functions. * Using external libraries like Lodash or Ramda, which provide optimized implementations of these operations. Keep in mind that the performance difference between these approaches will depend on the specific use case, dataset size, and JavaScript engine being used.
Related benchmarks:
filter-map vs reduce
filter-map vs reduce vs reduce with destructuring
filter-map vs reduce 2
Flat map + filter vs. Reduce
Reduce Push vs. flatMap vs 123
Comments
Confirm delete:
Do you really want to delete benchmark?