Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map vs Object Iteration
(version: 0)
Comparing performance of:
Map for..of vs Map for..of values() vs Object for..in vs Object for..of values()
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var map = new Map(); var obj = {}; for (var i = 0; i < 100000; i++) { map.set(`a${i}`, i); obj[`a${i}`] = i; }
Tests:
Map for..of
for (var item of map);
Map for..of values()
for (var item of map.values());
Object for..in
for (var item in obj);
Object for..of values()
for (var item in Object.values(obj));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Map for..of
Map for..of values()
Object for..in
Object for..of values()
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):
**Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark definition and results are used to test the performance of different iteration approaches in JavaScript. **Benchmark Definition** The benchmark definition consists of two parts: 1. **Script Preparation Code**: This code creates an empty Map object (`var map = new Map();`) and an empty object (`var obj = {};`). It then populates both objects with 100,000 key-value pairs using a `for` loop. 2. **Html Preparation Code**: This part is empty in the provided benchmark definition. **Individual Test Cases** The test cases are designed to measure the performance of different iteration approaches: 1. **Map for..of**: Iterates over the keys of the Map object using the `for...of` loop syntax (`for (var item of map);`) 2. **Map for..of values()**: Iterates over the values of the Map object using the `for...of` loop syntax with the `.values()` method (`for (var item of map.values());`) 3. **Object for..in**: Iterates over the properties (key-value pairs) of the Object object using the `for...in` loop syntax (`for (var item in obj);`) 4. **Object for..of values()**: Iterates over the values of the Object object using the `for...of` loop syntax with the `.values()` method (`for (var item in Object.values(obj));`) **Performance Comparison** The performance comparison is based on the number of executions per second for each test case. The results show that: * **Map for..of**: 9135.033203125 executions/second * **Map for..of values()**: 2931.1787109375 executions/second (approximately 32% slower than `for...of` on keys) * **Object for..in**: 97.29502868652344 executions/second (approximately 99% slower than `for...of` on keys) * **Object for..of values()**: 48.70204544067383 executions/second (approximately 85% slower than `for...of` on keys) **Pros and Cons of Different Approaches** 1. **Map for..of**: Fastest iteration approach, as it directly iterates over the keys of the Map object. 2. **Map for..of values()**: Slightly slower than `for...of` on keys, but still relatively fast. 3. **Object for..in**: Slowest iteration approach, as it iterates over all properties (key-value pairs) of the Object object. 4. **Object for..of values()**: Even slower than `Object for...in`, as it also has to iterate over the values. **Other Considerations** * Using `for...of` on keys is generally faster and more efficient than using `for...in` or accessing individual properties. * When iterating over values, using the `.values()` method can be slightly slower than accessing individual elements directly. * The performance difference between these approaches may vary depending on the specific use case and dataset. **Alternatives** Some alternative iteration approaches in JavaScript include: 1. **Array.prototype.forEach()**: Iterates over an array using a callback function. 2. **Array.prototype.map()**: Returns a new array with transformed elements from the original array. 3. **Array.prototype.filter()**: Returns a new array with filtered elements from the original array. These alternatives may have different performance characteristics and use cases compared to the `for...of` and `for...in` loop syntaxes used in this benchmark.
Related benchmarks:
iterating from a filled object VS iterating from a map
Map() vs Object() on write no strings
Map.prototype.forEach vs Array.prototype.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?