Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for loop: object vs map v3
(version: 0)
Comparing performance of:
for loop of Map vs for loop of Object
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var mapData = []; for (let i = 0; i < 1000000; i++) { mapData.push([i, `object${i}`]); } var map = new Map(mapData); var obj = {}; for (let i = 0; i < 1000000; i++) { obj[i] = `object${i}`; }
Tests:
for loop of Map
let sum = 0; for (let [key, val] of map) { sum += key; }
for loop of Object
let sum = 0; for (let [key, val] of Object.entries(obj)) { sum += key; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for loop of Map
for loop of Object
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):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition** The benchmark definition is represented by the `Script Preparation Code` section in the JSON file: ```json "Script Preparation Code": "var mapData = [];\r\nfor (let i = 0; i < 1000000; i++) {\r\n mapData.push([i, `object${i}`]);\r\n}\r\nvar map = new Map(mapData);\r\n \r\nvar obj = {};\r\nfor (let i = 0; i < 1000000; i++) {\r\n obj[i] = `object${i}`;\r\n}", ``` This script creates an array of pairs (`mapData`) and pushes it into a new Map object. It also creates an empty object (`obj`) and assigns the same values as in `mapData` to its properties using a nested loop. The purpose of this benchmark is to compare the performance of iterating over two data structures: a Map (a built-in JavaScript object that stores key-value pairs) and an Object (a custom-built data structure using arrays). **Options Compared** Two options are being compared: 1. **For loop of Map**: This test iterates over the values of the `map` object using the `for...of` loop. 2. **For loop of Object**: This test iterates over the properties of the `obj` object using the same `for...of` loop. **Pros and Cons** Both options have their pros and cons: * **For loop of Map**: + Pros: Maps are designed for fast iteration, and the `for...of` loop is optimized for this case. + Cons: This test relies on the specific implementation of the `Map` object in JavaScript engines. * **For loop of Object**: + Pros: Objects are more widely supported across different JavaScript engines, making this test more platform-independent. + Cons: Iterating over an array of properties can be slower than iterating directly over a Map. **Library and Purpose** No libraries are used in this benchmark. The `Map` object is a built-in part of the JavaScript language, while the `Object` is a custom-built data structure using arrays. **Special JS Feature or Syntax** The test uses the `for...of` loop, which is a modern JavaScript syntax introduced in ECMAScript 2015 (ES6). This feature allows iterating over iterable objects, such as Maps and Arrays. **Other Alternatives** To measure the performance of iterating over data structures, other alternatives could include: * Iterating directly over an array using `forEach` or indexing. * Using a library like Lodash to iterate over data structures. * Writing custom iteration functions using loops (e.g., `while`, `for` loops). Keep in mind that these alternatives might not provide the same level of performance as iterating directly over the data structure, especially for large datasets.
Related benchmarks:
JavaScript Array.prototype.map vs loop+Array.prototype.push ( big array )
for loop: object vs map
for loop: object vs map v2
Map.forEach vs Array.forEach vs Array.from(Map.prototype.values()).forEach
Comments
Confirm delete:
Do you really want to delete benchmark?