Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map & Set vs Reduce & Includes
(version: 0)
Comparing performance of:
Map & Set vs Reduce & Includes
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
filteredResults = [{ some: 'yes', profile: 'A'}, { some: 'yes', profile: 'A'}, { some: 'yes', profile: 'B'}, { some: 'C', profile: 'B'}, { some: 'yes', profile: 'B'}]
Tests:
Map & Set
const formattedProfiles = filteredResults.map(result => result.profile) const uniqueProfiles = [...new Set(formattedProfiles)]
Reduce & Includes
const formattedProfiles = filteredResults.reduce((uniqueProfiles, { profile }) => { return uniqueProfiles.includes(profile) ? uniqueProfiles : [...uniqueProfiles, profile]; }, []);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Map & Set
Reduce & Includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Map & Set
5303695.5 Ops/sec
Reduce & Includes
8919644.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided JSON represents two JavaScript microbenchmarks, "Map & Set" and "Reduce & Includes", which are used to compare the performance of two approaches for filtering unique values from an array. **Benchmark Definition** The benchmark definition is divided into two parts: Script Preparation Code and Html Preparation Code. The Script Preparation Code is a JavaScript snippet that generates an array of objects, `filteredResults`, with varying profile values. This code serves as input for both benchmarks. In the Benchmark Definition, we have: * **Map & Set**: A benchmark that uses the `map()` function to transform each object in `filteredResults` into its `profile` value and then converts the resulting array into a set using `new Set()`. Finally, it maps the set back into an array. * **Reduce & Includes**: A benchmark that utilizes the `reduce()` function to accumulate unique profiles. The accumulator `uniqueProfiles` is initialized as an empty array. For each profile in `filteredResults`, the code checks if it's already present in `uniqueProfiles` using the `includes()` method. If it is, the original value is ignored; otherwise, it's appended to `uniqueProfiles`. **Options Compared** The two benchmarks are comparing the following approaches: * **Map & Set**: This approach uses the `map()` function followed by a set data structure and then `map()` again to filter unique profiles. * **Reduce & Includes**: In this approach, the `reduce()` function is used with an initial value of an empty array. The `includes()` method is called on each profile in the input array. **Pros and Cons** ### Map & Set Pros: * **Efficient Memory Usage**: Sets automatically eliminate duplicate values, which can lead to improved memory efficiency. * **Fast Iteration**: Since sets only store unique values, iteration over them is generally faster than iterating over arrays with duplicates. Cons: * **Function Call Overhead**: The `map()` function and the subsequent set creation might incur additional overhead due to the number of function calls. ### Reduce & Includes Pros: * **Native Function Performance**: The `reduce()` function is a native JavaScript function, which can provide better performance compared to external libraries. * **Flexibility in Handling Duplicates**: By using the `includes()` method, this approach can handle duplicate values more flexibly than sets. Cons: * **Increased Memory Usage**: Since the accumulator needs to store all unique profiles, memory usage can be higher due to the accumulation of duplicate values. * **Potential Performance Hit from Includes()**: Iterating over an array with duplicates using `includes()` can incur additional overhead compared to directly working with a set. **Library and Special JS Features** In this benchmark, there is no explicit library mentioned. However, some features like `map()` and `reduce()` are part of the native JavaScript API. These functions are used for data transformation and aggregation in both benchmarks. There are no special JS features explicitly mentioned or utilized in this benchmark.
Related benchmarks:
filter.map vs reduce 2
Filter and Map vs Reduce
Reduce vs map with empty filter
Flat map + filter vs. Reduce
Comments
Confirm delete:
Do you really want to delete benchmark?