Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map from .reduce vs Map from .map vs Map from generator function
(version: 0)
Comparing performance of:
Map from .reduce vs Map from .map vs Map from generator function
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr10k = Array.from(Array(10000)).map((x, i) => ({ x1: i, x2: i * 2, x4: i * 4 })); function* getItems() { for (const x of arr10k) { yield [x.x1, x]; } }
Tests:
Map from .reduce
arr10k.reduce((acc, x) => acc.set(x.x1, x), new Map());
Map from .map
new Map(arr10k.map(x => [x.x1, x]));
Map from generator function
new Map(getItems());
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Map from .reduce
Map from .map
Map from generator function
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:135.0) Gecko/20100101 Firefox/135.0
Browser/OS:
Firefox 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Map from .reduce
3587.1 Ops/sec
Map from .map
2369.5 Ops/sec
Map from generator function
1413.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided JSON represents three microbenchmarks that test the performance of mapping over an array in JavaScript using different approaches: 1. **Map.from.reduce()**: This benchmark tests the performance of creating a Map from an array by reducing it, where each element of the array is mapped to another value. 2. **Map.from.map()**: This benchmark tests the performance of creating a Map from an array by mapping over the array and then passing the resulting array to the `Map` constructor. 3. **Map.from.generator function**: This benchmark tests the performance of creating a Map from an array using a generator function. **Options compared:** * `Map.from.reduce()`: uses the `reduce()` method to iterate over the array, where each element is mapped to another value and the resulting values are collected into a Map. * `Map.from.map()`: uses the `map()` method to create a new array with transformed elements, which is then passed to the `Map` constructor. * `Map.from.generator function`: uses a generator function (`getItems()`) to yield values from the array, which are then collected into a Map. **Pros and Cons:** 1. **Map.from.reduce()**: * Pros: concise and easy to read code. * Cons: may be less efficient than other approaches due to the overhead of the `reduce()` method. 2. **Map.from.map()**: * Pros: can be more readable and easier to understand for developers familiar with array transformations. * Cons: creates a new array, which can be memory-intensive for large datasets. 3. **Map.from.generator function**: * Pros: allows for lazy evaluation and potential performance benefits due to generator functions being less memory-intensive than arrays. * Cons: requires additional understanding of generators and their usage. In general, the choice between these approaches depends on the specific use case and performance requirements. If readability is more important than raw performance, `Map.from.map()` might be a good choice. However, if memory efficiency or raw performance are crucial, `Map.from.generator function` or `Map.from.reduce()` might be more suitable. **Library:** None of these benchmarks explicitly uses any external libraries beyond the standard JavaScript `Array`, `Map`, and `Generator` functions. **Special JS feature or syntax:** No special JavaScript features or syntaxes are used in these benchmarks.
Related benchmarks:
Map from .reduce vs Map from .map
Tim's reduce vs flatMap
flat map vs reduce concat
flatMap vs reduce, but without copying the array in each iteration
Comments
Confirm delete:
Do you really want to delete benchmark?