Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
map over Object (or Record in TS) vs Map
(version: 0)
Comparing performance of:
Obj / Object.entries() vs Map / spread vs Map / Array.from()
Created:
3 years ago
by:
Registered User
Jump to the latest result
Tests:
Obj / Object.entries()
let obj = {}; for (let i = 0; i < 10000; i++) { obj[i] = i; } let tuples = Object.entries(obj).map(([k, v]) => [k, v]);
Map / spread
let map = new Map(); for (let i = 0; i < 10000; i++) { map.set(i, i); } let tuples = [...map].map(([k, v]) => [k, v]);
Map / Array.from()
let map = new Map(); for (let i = 0; i < 10000; i++) { map.set(i, i); } let tuples = Array.from(map).map(([k, v]) => [k, v]);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Obj / Object.entries()
Map / spread
Map / Array.from()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Browser/OS:
Chrome 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Obj / Object.entries()
2975.8 Ops/sec
Map / spread
2676.8 Ops/sec
Map / Array.from()
2598.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases. **Benchmark Definition JSON** The provided JSON represents a benchmark definition, which includes: * `Name`: A unique name for the benchmark. * `Description`: An optional description of the benchmark (empty in this case). * `Script Preparation Code` and `Html Preparation Code`: These are empty strings, indicating that no specific code needs to be executed before running the benchmark. **Individual Test Cases** There are three test cases: 1. **Obj / Object.entries()** This test case uses JavaScript's built-in `Object.entries()` method to iterate over an object and convert its key-value pairs into an array of tuples. 2. **Map / spread** This test case creates a new Map, sets 10,000 key-value pairs, and then converts the map into an array using the spread operator (`[...]`). The resulting array is mapped over to create an array of tuples. 3. **Map / Array.from()** Similar to the previous test case, this one uses `Array.from()` to convert the map into an array before mapping over it. **Options Compared** The three test cases are comparing the performance of different approaches: * Using `Object.entries()` vs. using a Map and the spread operator (`[...]`). * Using `Array.from()` vs. using a Map and the spread operator (`[...]`). **Pros and Cons of Each Approach** Here's a brief overview of each approach: 1. **Obj / Object.entries()** * Pros: + Simple and efficient. + No additional memory allocation is required for the map. * Cons: + May not be as performant as using a Map, especially for large datasets. 2. **Map / spread** * Pros: + Can take advantage of modern JavaScript engines' ability to optimize map iterations. + Does not require additional memory allocation for the array. * Cons: + Requires creating an extra map object and iterating over it twice (once with `Object.entries()` and once again with `[...]`). 3. **Map / Array.from()** * Pros: + Similar to the previous approach, but avoids the need to iterate over the map twice. + Still takes advantage of modern JavaScript engines' optimization capabilities. * Cons: + Requires creating an extra array object. **Library and Purpose** In this benchmark, no external libraries are used. However, JavaScript's built-in `Map` data structure is utilized in all test cases. **Special JS Feature or Syntax** None of the test cases use any special JavaScript features or syntax beyond what is commonly available in modern JavaScript implementations. **Alternatives** Other approaches to iterate over objects and maps could include: * Using a custom iterator function. * Utilizing a library like Lodash's `mapKeys()` or `mapValues()`. * Implementing a simple loop using indices (e.g., `for (let i = 0; i < obj.length; i++) { ... }`). Keep in mind that these alternatives might not be as efficient or optimized for modern JavaScript engines, but they can provide alternative perspectives on the problem.
Related benchmarks:
flatMap vs map/flat
flatMap vs filter + map
new Map vs set array to map
Array Spread vs Fill vs New Array
Object.fromEntries vs Map
Comments
Confirm delete:
Do you really want to delete benchmark?