Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array vs Map vs object iteration 2
(version: 0)
Comparing performance of:
Array.forEach vs Array for...of vs Map.forEach vs object for...in
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = [] var map = new Map() var object = {} for (var i = 0; i < 2000; i++) { array[i] = i; map.set(i.toString(), i); object[i.toString()] = i; } var results = []
Tests:
Array.forEach
array.forEach(i => { results.push(i * 2) })
Array for...of
for (const i of array) { results.push(i * 2) }
Map.forEach
map.forEach(i => { results.push(i * 2) })
object for...in
for (const i in object) { results.push(object[i] * 2) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Array.forEach
Array for...of
Map.forEach
object for...in
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):
**Benchmark Overview** The provided benchmark measures the performance of three different iteration approaches in JavaScript: `Array.forEach`, `Array for...of`, and `Map.forEach` vs. `Object for...in`. The benchmark creates an array, a Map, and an object with 2000 elements each, and then uses these data structures to iterate over them and perform calculations. **Iteration Approaches** 1. **Array.forEach**: This approach uses the `forEach` method of arrays in JavaScript, which iterates over the elements of an array by calling a provided callback function once for each element. 2. **Array for...of**: This approach uses the `for...of` loop with an iterator that can iterate over an array. It provides a more modern and concise way to iterate over arrays compared to traditional `forEach`. 3. **Map.forEach**: Similar to Array forEach, this approach uses the `forEach` method of Maps in JavaScript, which iterates over the key-value pairs of a Map. 4. **Object for...in**: This approach uses the `for...in` loop with an iterator that can iterate over the properties (key-value pairs) of an object. **Comparison** The benchmark compares the performance of these four approaches: * **Array forEach vs. Array for...of**: The modern and concise `for...of` loop is expected to be faster than the traditional `forEach` method, as it avoids the overhead of a callback function. * **Map forEach vs. Object for...in**: Map `forEach` is generally faster than object iteration using `for...in`, as it uses a more efficient iterator that can skip over non-enumerable properties. **Pros and Cons** 1. **Array forEach**: * Pros: widely supported, easy to use. * Cons: slower compared to `for...of`. 2. **Array for...of**: * Pros: modern, concise, faster than traditional `forEach`. * Cons: may not be as widely supported in older browsers or JavaScript engines. 3. **Map forEach**: * Pros: efficient, fast, and suitable for large datasets. * Cons: less intuitive compared to array iteration. 4. **Object for...in**: * Pros: easy to understand, works well with objects. * Cons: slower compared to Map `forEach`, skips non-enumerable properties. **Library Usage** None of the test cases explicitly use any libraries, but it's worth noting that some JavaScript engines or browsers may have optimized implementations for certain methods (e.g., WebAssembly for performance-critical code). **Special JS Features/Syntax** The benchmark uses the `for...of` loop with an iterator, which is a modern JavaScript feature introduced in ECMAScript 2015. This loop provides a more concise way to iterate over arrays and other iterable objects. **Alternatives** Other alternatives for iterating over data structures include: * **Lodash's `forEach`**: A popular utility library that provides a robust implementation of the `forEach` method. * **Underscore.js's `forEach`**: Another popular utility library that provides a more concise implementation of the `forEach` method. * **Manual iteration using `for` loops**: An alternative approach to iteration that uses traditional `for` loops, which can be more verbose but still effective. In summary, the benchmark highlights the performance differences between various iteration approaches in JavaScript and encourages developers to consider the trade-offs between readability, maintainability, and performance when choosing an iteration method.
Related benchmarks:
map vs forEach Chris
map vs forEach Chris v2b
Array.forEach vs Object.keys().forEach
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?