Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map.foreach vs Map.for..of with func
(version: 0)
Compare loop performance
Comparing performance of:
foreach vs for..of
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var mapV = new Map([[1, 1], [2, 1],[3, 1],[4, 1],[5, 1],[6, 1],[7, 1],[8, 1],[9, 1],[10, 1],[11, 1],[12, 1],[13, 1],[14, 1],[15, 1],[16, 1],[17, 1],[18, 1],[19, 1],[20, 1],[21, 1],[22, 1],[23, 1],[24, 1],[25, 1]]);
Tests:
foreach
mapV.forEach(function(value, key) { console.info(value + key); });
for..of
for (var [key, value] of mapV) { console.info(value + key); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
foreach
for..of
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36
Browser/OS:
Chrome 140 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
foreach
9163.4 Ops/sec
for..of
7938.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. The provided JSON represents a benchmark test that compares the performance of two approaches for iterating over a Map object in JavaScript: `Map.forEach` and `Map.for..of with func`. **Test Comparison** In this test, we have two individual test cases: 1. **foreach**: This test uses the traditional `forEach` method to iterate over the Map object. 2. **for..of**: This test uses the new `for...of` loop syntax, which is designed for iterating over iterable objects like Maps. **Options Compared** The two options being compared are: * **Traditional `forEach`**: This approach uses a callback function to process each item in the Map. * **New `for...of` with func**: This approach uses a new syntax for iterating over an iterable object, which is designed to be more efficient and readable. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **Traditional `forEach`**: + Pros: Wide support across older browsers and versions. + Cons: Can be slower than the new `for...of` syntax due to the overhead of function calls. * **New `for...of` with func**: + Pros: Faster performance, more concise code, better support for modern browsers and JavaScript engines. + Cons: May not work in older browsers or versions that don't support the new syntax. **Library** None of the test cases use any external libraries. The Map object is a built-in JavaScript data structure. **Special JS Feature/Syntax** The `for...of` loop syntax is a relatively recent addition to JavaScript, introduced in ECMAScript 2015 (ES6). It's designed for iterating over iterable objects like Maps, Sets, and arrays. **Benchmark Preparation Code** The provided preparation code creates a large Map object with 30 key-value pairs. This Map object is used as the input for both test cases. **Other Alternatives** If you're looking for alternative approaches to iterate over a Map object in JavaScript, here are some options: * **`Array.prototype.forEach.call(map)`**: This approach uses the `forEach` method on an array wrapper around the Map object. * **`map.entries()`**: This approach uses the `entries()` method on the Map object to get an iterator over its key-value pairs. * **Manual iteration using a loop**: You can also write a custom loop to iterate over the Map object, but this is generally considered less readable and less efficient than the other options.
Related benchmarks:
map vs for: too much data
Object.values Array.prototype.map vs Lodash.map
Object.entries Array.prototype.map vs Lodash.map
Large containers non existing Array.includes vs Set.has vas Map.has
Comments
Confirm delete:
Do you really want to delete benchmark?