Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map vs Object - string keys - read performance
(version: 0)
Comparing performance of:
Read from map vs Read from obj
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
window.myMap = new Map(); window.myObj = new Map(); window.generateObj = () => ({ 'a': Math.random() * 10000, 'b': Math.random() * 10000, 'c': Math.random() * 10000 }); for (let i = 0; i < 1000; i++) { let id = 'id-' + i; let obj = generateObj(); myMap.set(id, obj); myObj[id] = obj; }
Tests:
Read from map
let id = `id-` + Math.floor(Math.random() * 1000); let value = myMap.get(id);
Read from obj
let id = `id-` + Math.floor(Math.random() * 1000); let value = myObj[id];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Read from map
Read from obj
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 JSON and explain what's being tested. **Benchmark Definition** The benchmark measures the performance of reading values from two data structures: `Map` and `Object`. Both data structures are created with string keys, but they differ in how the keys are generated. The key generation is done using template literals (`id-` + random number). **Options Compared** There are two options compared: 1. **Reading from a Map**: The benchmark defines a function `generateObj()` that returns an object with three properties (key-value pairs). This object is then set on the `Map` instance using the generated key. 2. **Reading from an Object**: The same `generateObj()` function is used, but its return value is assigned directly to the `Object` instance using the generated key. **Pros and Cons of Each Approach** **Map** Pros: * Maps are designed for fast lookups, especially when keys are unique. * Template literals can be more readable and efficient than concatenation. Cons: * Maps require a specific type of key (string) to be used as a key. * If the key is not present in the map, `get()` returns `undefined`. **Object** Pros: * Objects allow for any data type as keys, making them more flexible. * Assigning values to an object using a key-value pair syntax can be easier to read. Cons: * Objects are generally slower than maps when it comes to lookups. * Using template literals may lead to performance issues if the browser doesn't optimize them correctly. **Library and Special JS Features** The `Map` and `Object` instances are part of the JavaScript standard library. No special libraries or features are used in this benchmark. **Other Considerations** This benchmark is likely intended to test the performance differences between maps and objects when it comes to fast lookups. The use of template literals and random key generation adds an extra layer of complexity, making it a more challenging benchmark. If you want to simulate similar scenarios, you can create additional benchmark definitions with different options (e.g., reading from arrays or sets) to test the performance of various JavaScript data structures. Other alternatives for measuring performance in JavaScript benchmarks include: * Using `Date.now()` and manual timing loops. * Leveraging existing libraries like Benchmark.js or PerfTest. * Creating custom timers using Web APIs like `performance.now()` or `requestAnimationFrame()`. * Utilizing framework-specific performance testing tools, such as React's `react-dom/test-utils` or Angular's `@angular/core/testing`.
Related benchmarks:
Object vs Map lookup w/ rando integer keys
Object vs Map lookup w/ rando integer key
Object vs Map lookup w/ rando integer key and array
Object vs Map lookup with int keys
Comments
Confirm delete:
Do you really want to delete benchmark?