Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map vs Object vs Reduce
(version: 3)
Comparing performance of:
Numbers Map vs Numbers Object vs Strings Map vs Strings Object vs Numbers Object Reduce vs Strings Object Reduce
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var numberSet = new Array(10000).fill(0).map(() => Math.floor(Math.random() * 20)) var stringSet = new Array(10000).fill(0).map(() => Math.random().toString(16).slice(2, 7)) var numberMap = new Map(); var stringMap = new Map(); var numberObject = {}; var stringObject = {};
Tests:
Numbers Map
numberSet.forEach((num) => numberMap.set((numberMap.get(num) || 0) + 1))
Numbers Object
numberSet.forEach((num) => numberObject[num] = (numberObject[num] || 0) + 1)
Strings Map
stringSet.forEach((num) => stringMap.set((stringMap.get(num) || 0) + 1))
Strings Object
stringSet.forEach((num) => stringObject[num] = (stringObject[num] || 0) + 1)
Numbers Object Reduce
numberSet.reduce((obj, num) => { obj[num] = (obj[num] || 0) + 1; return obj; }, {})
Strings Object Reduce
stringSet.reduce((obj, string) => { obj[string] = (obj[string] || 0) + 1; return obj; }, {})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
Numbers Map
Numbers Object
Strings Map
Strings Object
Numbers Object Reduce
Strings Object 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
gemma2:9b
, generated one year ago):
This benchmark compares different methods for counting the occurrences of unique numbers and strings in two arrays: `numberSet` and `stringSet`. Let's break down the options being tested: **1. Maps:** * **Numbers Map (`Numbers Map` test):** Iterates through `numberSet`, using `forEach` to set each number as a key in the `numberMap`. If a number already exists, its count is incremented; otherwise, it's added with a count of 1. * **Strings Map (`Strings Map` test):** Similar to the numbers map, but works with `stringSet`. **Pros:** * **Efficient for lookups and insertions:** Maps are designed for fast key-based access and insertion operations. * **Auto-handles uniqueness:** You don't need explicit checks for unique keys. **Cons:** * **Not as intuitive as objects:** Some developers might find the syntax less familiar than using object properties directly. **2. Objects:** * **Numbers Object (`Numbers Object` test):** Iterates through `numberSet`, using `forEach` to set each number as a key in the `numberObject`. Similar to the maps, it increments existing counts or initializes them to 1 if a new number is encountered. * **Strings Object (`Strings Object` test):** Same logic as the numbers object but with `stringSet`. **Pros:** * **Familiar syntax:** Many developers are comfortable working with objects and their properties. * **Concise for simple tasks:** Can be more compact than using maps in some scenarios. **Cons:** * **Less efficient for large datasets:** Accessing and modifying object properties can be slower than working with Maps, especially when dealing with many keys. **3. `reduce`:** * **Numbers Object Reduce (`Numbers Object Reduce` test):** Uses the `reduce` method to iterate through `numberSet`. It builds an object (`obj`) where the keys are the numbers from the array and the values are their counts. * **Strings Object Reduce (`Strings Object Reduce` test):** Same logic as the numbers reduce but with `stringSet`. **Pros:** * **Functional programming style:** `reduce` promotes a functional approach to data manipulation, which can be elegant and expressive. **Cons:** * **Can be less readable than iteration:** The functional nature of `reduce` might not be immediately intuitive for all developers. **Benchmark Results Interpretation:** The results show that using Maps generally performs the best for counting occurrences. While Objects are familiar, they can be slower. `reduce`, while powerful, sometimes falls behind in terms of raw speed. Remember, the optimal choice depends on factors like dataset size, performance requirements, and personal coding style.
Related benchmarks:
reduce (immutable) vs map + fromEntries
flatMap + reduce vs reduce + reduce
Math.max vs Array.reduce
flat map vs reduce concat
Flatmap vs reduce with objects
Comments
Confirm delete:
Do you really want to delete benchmark?