Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object vs Map lookup w/ rando integer key and array
(version: 0)
Comparing the performance of lookups in large objects vs large maps when the keys are integers
Comparing performance of:
Map Lookup vs Object Lookup
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
window.m = new Map(); window.o = {}; for (let i = 0; i < 2000; ++i) { window.m.set(Math.random(100000), [i, i + 1]); window.o[Math.random(100000)] = [i, i + 1]; }
Tests:
Map Lookup
for (let i = 0; i < 10000; ++i) { if (window.m.get( Math.random(100000)) !== 1) { } }
Object Lookup
for (let i = 0; i < 10000; ++i) { if (window.o[Math.random(100000)] !== 1) { } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Map Lookup
Object Lookup
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 is testing two different approaches to lookup performance in JavaScript: 1. **Map Lookup**: This test uses a `Map` data structure to store key-value pairs. The map is populated with random integers as keys and arrays containing two values (`i` and `i+1`) as values. 2. **Object Lookup**: This test uses an object literal (`window.o = {}`) to store similar key-value pairs. **Comparison** The benchmark is comparing the performance of these two approaches on a large dataset (2000 entries). The test measures the execution time for each approach over 10,000 iterations. **Options Compared** There are two main options being compared: * **Map Lookup**: Using a `Map` data structure to store key-value pairs. * **Object Lookup**: Using an object literal (`window.o = {}`) to store key-value pairs. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **Map Lookup** + Pros: - Fast lookup times due to the use of hash-based indexing. - Efficient memory usage, as only the required data is stored. + Cons: - Requires JavaScript engines that support `Map` objects (most modern browsers do). - May have overhead due to the need to allocate and manage a new object. * **Object Lookup** + Pros: - Wide compatibility with older browsers, as objects are a built-in data structure in JavaScript. - No additional memory allocation or management required. + Cons: - Slower lookup times compared to `Map` due to the need for linear searching. **Library Used** The `Map` object is used as part of the standard library in most modern JavaScript engines, including those used by Google Chrome. The `Array.prototype.includes()` method is also used in both benchmarks. **Special JS Feature/Syntax** There are no special JavaScript features or syntax mentioned in this benchmark. However, it's worth noting that using a `Map` object requires support for the `Map` data structure, which was introduced in ECMAScript 2015 (ES6). **Other Alternatives** If you want to explore alternative approaches, here are a few options: * **Array Indexing**: Instead of using a map or object, you could use an array with numerical indices. This approach would eliminate the need for hash-based indexing but would still require linear searching. * **Sets**: Another data structure that could be used instead of maps is sets. Sets provide fast lookup times and are suitable for this benchmark. Keep in mind that these alternatives may have different performance characteristics or compatibility issues compared to using `Map` objects.
Related benchmarks:
Object vs Map lookup w/ rando integer keys
Object vs Map lookup w/ rando integer key
Object vs Map lookup: random integer key
Object vs Map lookup with int keys
Comments
Confirm delete:
Do you really want to delete benchmark?