Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map vs Object.entries
(version: 0)
only on a small collection
Comparing performance of:
map forEach vs map for of vs Object.entries for of
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { 1: "ASDASDASDASD", 2: "ASDASDASDASDASDA", 3: "ASDASDASDASDAAA", 4: 5242, 5: "AAAAAA", 6: "asdasdasdasd", 7: [24342343, "sweet", "hello"], 8: [1, 2, 3, 4], 9: [1, 2, 3, 4, 5], 10: "AAA", 11: 5545, 12: "gutenTag" } var map = new Map(Object.entries(obj))
Tests:
map forEach
let total = 0 map.forEach((k, v) => { total += 1 })
map for of
let total = 0 for (const [key, val] of map){ total += 1 }
Object.entries for of
let total = 0 for (const [key, val] of Object.entries(obj)){ total += 1 }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
map forEach
map for of
Object.entries for of
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 YaBrowser/24.7.0.0 Safari/537.36
Browser/OS:
Yandex Browser 24 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
map forEach
9367864.0 Ops/sec
map for of
14180739.0 Ops/sec
Object.entries for of
2350248.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested on the provided JSON. The benchmark is comparing three approaches to iterate over an object: 1. `Map.forEach()` 2. `for...of` loop with `map` 3. `Object.entries()` method with `for...of` loop **Map.forEach():** This approach uses the `Map` data structure, which is a collection of key-value pairs. The `forEach()` method allows you to iterate over the map's entries. Pros: * Efficient for large datasets, as it avoids unnecessary object property lookups. * Native JavaScript implementation, so it's optimized for performance. Cons: * May not work as expected with complex data structures or edge cases. * Not supported in older browsers or Node.js versions. **For...of loop with map:** This approach uses a `for...of` loop to iterate over the `map` object. The syntax is similar to iterating over an array, but it's more concise and readable for objects. Pros: * Easy to understand and implement. * Works well with most data structures, including objects. * Supported in all modern browsers and Node.js versions. Cons: * May not be as efficient as `forEach()` for very large datasets. * Requires creating a new `map` object from the original data structure. **Object.entries() method with for...of loop:** This approach uses the `Object.entries()` method to get an array of key-value pairs from the original object. Then, it iterates over this array using a `for...of` loop. Pros: * Works well with objects that are not necessarily maps. * Easy to understand and implement. * Supported in all modern browsers and Node.js versions. Cons: * May not be as efficient as `forEach()` for large datasets, since it involves creating an array of key-value pairs. * Requires two extra operations (creating the array and iterating over it). Now, let's discuss some special JavaScript features used in this benchmark: * **For...of loop**: This is a modern JavaScript feature that allows you to iterate over arrays or iterables using a concise syntax. It was introduced in ECMAScript 2015. * **Map**: The `Map` data structure was introduced in ECMAScript 2015 and provides a more efficient way to work with key-value pairs than traditional objects. The benchmark also uses some special strings: * **RawUAString**: This is the User Agent string of the browser that ran the test. It provides information about the browser's version, platform, and operating system. * **Browser**: The name of the browser that ran the test. Now, let's talk about other alternatives for this benchmark: * **Array.prototype.forEach()**: Instead of using `Map.forEach()`, you could use `Array.prototype.forEach()` to iterate over an array. However, since the original data structure is not necessarily an array, this approach would require additional steps. * **Traditional object iteration**: You could also use traditional object iteration methods like `for...in` or `Object.keys()` to iterate over the object's properties. However, these approaches are less efficient and less readable than the ones used in the benchmark. In summary, the benchmark is comparing three approaches to iterate over an object: `Map.forEach()`, for...of loop with map, and Object.entries() method with for...of loop. Each approach has its pros and cons, and the best choice depends on the specific use case and performance requirements.
Related benchmarks:
map vs fromentries 2
entries vs keys lookup
Map convert
Object.entries VS Object.keys with obj[key]
Object.fromEntries vs Map
Comments
Confirm delete:
Do you really want to delete benchmark?