Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
ES6 Map - forEach vs for...of while clearing
(version: 0)
Comparing performance of:
forEach vs for...of
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var myMap = new Map([[x => x, 'a'], [x => x, 'b'], [x => x, 'c'], [x => x, 'd']]);
Tests:
forEach
myMap.forEach((payload, callback) => { callback(payload); myMap.delete(callback); });
for...of
for (const [callback, payload] of myMap) { callback(payload); myMap.delete(callback); }
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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided JSON represents a benchmark test case for JavaScript microbenchmarks on the MeasureThat.net website. The test aims to compare the performance of two approaches for iterating over and modifying elements in a Map data structure. **Overview** A Map is a built-in JavaScript object that stores key-value pairs, where each key is unique and maps to a specific value. In this benchmark, we're comparing two ways to iterate over the key-value pairs in a Map: 1. **forEach**: This method iterates over the elements of an array or an object using a callback function. 2. **for...of**: This statement allows iterating over iterable objects, such as arrays and Maps. **Options compared** The benchmark compares the performance of these two approaches: * **forEach**: Iterates over the key-value pairs in the Map using a callback function. * **for...of**: Iterates over the key-value pairs in the Map using the for...of statement. **Pros and Cons** ### forEach Pros: * Widely supported across browsers and environments. * Allows for direct access to the current iteration's value and index (not applicable in this case, as we're only accessing values). Cons: * Can be slower due to the overhead of function calls and potential bounds checks. ### for...of Pros: * Can be faster due to fewer function call overheads. * More concise and readable syntax. Cons: * Not all browsers support it (e.g., older versions of Internet Explorer). * May not work correctly if the Map object's prototype chain is modified. **Library** There is no external library used in this benchmark. The Map data structure is a built-in JavaScript object. **Special JS feature or syntax** None are mentioned explicitly, but `=>` is used as an arrow function syntax (i.e., shorthand for function expressions). **Benchmark preparation code** The script preparation code creates a new Map instance with four key-value pairs and assigns it to the variable `myMap`. The HTML preparation code is empty. **Individual test cases** There are two test cases: 1. **forEach**: Iterates over the key-value pairs in `myMap` using the `forEach` method. 2. **for...of**: Iterates over the key-value pairs in `myMap` using the for...of statement. **Latest benchmark result** The latest benchmark results show that the **for...of** approach is slightly faster than the **forEach** approach, with an average of 14,206,932 executions per second (in Chrome 106 on a Mac OS X 10.15.7 desktop) compared to 14,245,993 executions per second. **Other alternatives** If you want to explore other approaches for iterating over Map elements, consider the following: * **Array.from()**: This method converts a Map into an array and allows iteration using standard array methods (e.g., forEach). * **Iterators**: You can create custom iterators for Maps using the `Iterator` class from JavaScript's built-in `Symbol.iterator` protocol. * **Generators**: You can use generator functions to iterate over Map elements, which may provide better performance in certain scenarios. Keep in mind that these alternatives might have trade-offs in terms of code readability, compatibility with older browsers, or overall performance.
Related benchmarks:
map vs forEach Chris
map vs forEach Chris v2
map vs forEach Chris v2b
JS Map foreach vs for of
Array fill map, vs for i loop
Comments
Confirm delete:
Do you really want to delete benchmark?