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
llama3.1:latest
, generated one year ago):
Let's break down the benchmark test case. **What is being tested?** The test case compares the performance of four different approaches to iterate over an array and update a collection (either a Map or an Object) with counts for each element: 1. **Numbers Map**: Updating a Map with counts for numbers using `Map.set()` and `Map.get()`. 2. **Numbers Object**: Updating an Object with counts for numbers using object literal syntax. 3. **Strings Map**: Updating a Map with counts for strings using `Map.set()` and `Map.get()`. 4. **Strings Object Reduce**: Using the `reduce()` method to update an Object with counts for strings. **Options compared** The test case compares two main options: updating a Map vs updating an Object. Within each option, there are two sub-options: * For Maps: + Numbers Map: Updating a Map with counts for numbers. + Strings Map: Updating a Map with counts for strings. * For Objects: + Numbers Object: Updating an Object with counts for numbers using object literal syntax. + Strings Object Reduce: Using the `reduce()` method to update an Object with counts for strings. **Pros and cons of each approach** Here's a brief summary: * **Map**: + Pros: Efficient iteration, fast lookups, and updates. + Cons: Requires creating and managing a separate Map object. * **Object**: + Pros: Simple to use, no need to create a separate object. + Cons: Iteration can be slow, especially for large objects. The `reduce()` method is also being compared in the Strings Object Reduce test case. While not directly comparable to the other options, it's worth noting that `reduce()` is generally slower than using `Map` or Object literals due to its iterative nature. **Libraries and features** No external libraries are used in this test case. **Special JS feature or syntax** The `reduce()` method is a built-in JavaScript function that takes an array and returns a single value (in this case, an updated Object). No special JS features or syntax are being used in this test case. **Alternatives** Other alternatives to update counts for numbers and strings could be: * Using an Array with indices as keys (e.g., `array[num] = count`). * Creating a custom data structure, such as a hash table. * Utilizing external libraries like Lodash or Underscore.js that provide similar functionality. Keep in mind that these alternatives may have their own performance characteristics and use cases.
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?