Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Yet another bad Map vs Object read benchmark
(version: 0)
Comparing performance of:
object read vs map read
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var n = 730; var entries = [...Array(n)].map((_, i) => ['k' + i, i]) ; var m = new Map(entries); var o = Object.fromEntries(entries);
Tests:
object read
let ro = ""; for (let i = 0, len = entries.length; i < entries.length; ++i) { ro += o[entries[i][0]]; }
map read
let rm = ""; for (let i = 0, len = entries.length; i < entries.length; ++i) { rm += m.get(entries[i][0]); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
object read
map read
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):
I'll do my best to explain the benchmark and its various aspects. **What is being tested?** The provided benchmark tests two ways of reading data from an object (or a Map) in JavaScript: using Object.fromEntries() and using Map.prototype.get(). The test measures the performance difference between these two approaches, specifically how fast each method can concatenate the values associated with the keys. **Options compared** There are only two options being compared: 1. **Object.read**: This approach uses `Object.fromEntries()` to create an object from a given array of key-value pairs, and then iterates over the object's entries using a for loop to concatenate the values. 2. **Map.read**: This approach creates a new Map from the same array of key-value pairs and uses the Map.prototype.get() method to retrieve each value associated with its corresponding key. **Pros and Cons** * **Object.read**: * Pros: It's a more intuitive way to read data from an object, as it uses a familiar JavaScript method. However, this approach is slower than Map.read due to the overhead of creating and manipulating an object. * Cons: It's less efficient because of the object creation process and string concatenation. * **Map.read**: * Pros: It's faster and more memory-efficient since it doesn't create a new object, but instead uses a specialized data structure designed for key-value pairs. This approach is also more concise and easier to read. * Cons: The Map.prototype.get() method may not be as familiar or intuitive to some developers. **Library usage** In the provided benchmark code, no external library is used other than JavaScript's built-in `Array`, `Map`, and `Object` methods. However, it's essential to note that using libraries or frameworks can significantly impact performance. **Special JavaScript features/syntax** None of the special JavaScript features or syntax are being tested in this benchmark. It only focuses on the differences between using objects and Maps for key-value pairs. **Other alternatives** If you're looking for alternative ways to read data from an object, some possible approaches include: * Using `for...in` loops with `Object.keys()` or `Object.getOwnPropertyNames()`. * Utilizing libraries like Lodash's `mapValues()` function. * Leveraging the `reduce()` method to concatenate values. Keep in mind that each of these alternatives may have its own trade-offs in terms of performance, readability, and maintainability.
Related benchmarks:
iterating from a filled object VS iterating from a map
Array from() vs Map.keys()
Object spread vs New map with string keys
Object spread vs New map entries
Object.fromEntries vs Map
Comments
Confirm delete:
Do you really want to delete benchmark?