Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Number keyed maps
(version: 3)
Comparing performance of:
sparse array, int keys vs object, int keys vs Map, int keys vs sparse array, float keys vs object, float keys vs Map, float keys
Created:
5 years ago
by:
Registered User
Jump to the latest result
Tests:
sparse array, int keys
const a = []; for (let i = 0; i < 100; i++) a[Math.random() * 10000 | 0] = 1; let size = 0; for (const item of a) size++;
object, int keys
const o = {}; for (let i = 0; i < 100; i++) o[Math.random() * 10000 | 0] = 1; let size = 0; for (const item in o) size++;
Map, int keys
const m = new Map(); for (let i = 0; i < 100; i++) m.set(Math.random() * 10000 | 0, 1); let size = 0; for (const item of m.entries()) size++;
sparse array, float keys
const a = []; for (let i = 0; i < 100; i++) a[Math.random() * 10000] = 1; let size = 0; for (const item of a) size++;
object, float keys
const o = {}; for (let i = 0; i < 100; i++) o[Math.random() * 10000] = 1; let size = 0; for (const item in o) size++;
Map, float keys
const m = new Map(); for (let i = 0; i < 100; i++) m.set(Math.random() * 10000, 1); let size = 0; for (const item of m.entries()) size++;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
sparse array, int keys
object, int keys
Map, int keys
sparse array, float keys
object, float keys
Map, float keys
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 dive into the world of JavaScript microbenchmarks on MeasureThat.net. The benchmark being tested is about different data structures (arrays and objects) with either integer or float keys, and their performance in various iteration methods (e.g., `for`, `in`, `entries()`, etc.). The goal is to compare the execution times of these different approaches to see which one is the most efficient. **Options being compared:** 1. **Arrays**: Using integers as keys for sparse arrays (`const a = [];\r\nfor (let i = 0; i < 100; i++) a[Math.random() * 10000 | 0] = 1;`) and dense arrays (`const a = [];\r\nfor (let i = 0; i < 100; i++) a[Math.random() * 10000] = 1;`). 2. **Objects**: Using integers as keys for sparse objects (`const o = {};\r\nfor (let i = 0; i < 100; i++) o[Math.random() * 10000 | 0] = 1;`) and dense objects (`const o = {};\r\nfor (let i = 0; i < 100; i++) o[Math.random() * 10000] = 1;`). 3. **Maps**: Using integers as keys for sparse maps (`const m = new Map();\r\nfor (let i = 0; i < 100; i++) m.set(Math.random() * 10000 | 0, 1);`) and dense maps (`const m = new Map();\r\nfor (let i = 0; i < 100; i++) m.set(Math.random() * 10000, 1);`). **Pros and Cons:** * **Arrays**: + Pros: Simple and lightweight, easy to access elements by index. + Cons: Can be less efficient than objects or maps for iteration, especially when dealing with large datasets. * **Objects**: + Pros: Allows for flexible key-value pairs, can be more efficient than arrays for certain use cases (e.g., string lookups). + Cons: Can be slower than arrays or maps for direct access and indexing. * **Maps**: + Pros: Provides a fast and efficient way to store key-value pairs, especially when iterating over the keys. + Cons: May have higher memory overhead due to the extra metadata required. **Library usage:** None of the benchmark test cases use any external libraries. **Special JS features or syntax:** * **Arrow functions**: Used in some of the benchmark test cases (e.g., `for (const item of m.entries()) size++;`). * **Template literals**: Not used in this benchmark. * **Rest parameters**: Not used in this benchmark. * **Spread syntax**: Not used in this benchmark. **Other alternatives:** If you'd like to create similar benchmarks, consider using MeasureThat.net's API or creating your own JavaScript benchmarking framework. Additionally, you can explore other data structures and iteration methods to compare their performance in different scenarios. Keep in mind that the specific results of this benchmark may vary depending on the JavaScript engine, version, and hardware being used.
Related benchmarks:
Map vs WeakMap (real-world) Performance
Long int keys (19 digits)
Long int keys (19 digits) (one select)
Map vs Object (real-world) Performance (better)
Map vs Object (real-world) Performance (non-numeric key)
Comments
Confirm delete:
Do you really want to delete benchmark?