Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map.prototype.forEach vs Array.prototype.forEach
(version: 0)
Comparing performance of:
Map.prototype.forEach vs Array.prototype.forEach
Created:
2 years ago
by:
Guest
Go to the latest result
Script Preparation code:
var arr = []; var map = new Map(); for (let i=0; i<100000; i++) { arr.push(i); map.set(i, i); }
Tests:
Map.prototype.forEach
map.forEach(console.log);
Array.prototype.forEach
arr.forEach(console.log);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Map.prototype.forEach
Array.prototype.forEach
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years 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
Array.prototype.forEach
4/s
Map.prototype.forEach
4/s
View exact numbers
Test name
Executions per second
✓
Array.prototype.forEach
4 Ops/sec
Map.prototype.forEach
4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript benchmarks! **What is being tested?** The provided JSON represents two individual test cases: 1. `Map.prototype.forEach`: This tests how fast the `forEach` method on Map objects executes. In this case, it logs each value in the map using `console.log`. 2. `Array.prototype.forEach`: This tests how fast the `forEach` method on Array objects executes. It achieves the same result as the previous test, but with an array instead of a map. **Options being compared** The benchmark compares the performance of two options: A) Using `Map.prototype.forEach` to iterate over a map. B) Using `Array.prototype.forEach` to iterate over an array. **Pros and Cons of each approach:** 1. **Using `Map.prototype.forEach`**: * Pros: + Can be more efficient for large datasets, as it uses a more optimized iteration mechanism. + Can provide better cache locality, leading to faster execution times. * Cons: + May not work correctly with non-numeric keys in maps (this is a known limitation). 2. **Using `Array.prototype.forEach`**: * Pros: + Works with both numeric and non-numeric keys in arrays. + Is widely supported and well-maintained. * Cons: + May be slower than using `Map.prototype.forEach` for large datasets. **Library usage** In this benchmark, the following libraries are used: 1. None explicitly mentioned, but JavaScript's built-in `Map` and `Array` objects are used. **Special JS feature or syntax** There is no special JS feature or syntax being tested in these individual test cases. However, it's worth noting that other benchmarks might involve features like async/await, Promises, or WebAssembly, which would require additional setup and consideration. **Benchmark preparation code** The provided `Script Preparation Code` initializes an empty array `arr` and a new Map object `map`, and then iterates 100,000 times, pushing values to the array and setting key-value pairs in the map. This creates a large dataset for the benchmark. **Other alternatives** If you wanted to test other approaches or libraries, some potential alternatives could include: * Using `Set.prototype.forEach` instead of `Map.prototype.forEach` * Comparing different iteration mechanisms, such as `for...in`, `for...of`, or `while` loops * Testing the performance of custom iteration functions or higher-order functions (e.g., `forEach` created using a lambda function) * Incorporating other libraries like Lodash or Ramda to compare their performance with JavaScript's built-in methods Keep in mind that different benchmarks might focus on specific aspects of performance, such as cache locality, memory allocation, or parallelism.
Related benchmarks
map vs forEach Chris v2b
Map.forEach vs Array.forEach vs Array.from(Map.prototype.values()).forEach
Map.forEach vs Array.forEach vs Array.from(Map.values()).forEach
Comments
Confirm delete:
Do you really want to delete benchmark?