Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
map vs obj vs array
(version: 0)
Comparing performance of:
map vs array vs obj
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var i = 0, count = 100000, a; var map = new Map(); var array = []; var obj = {}; for (i = 0; i < count; i++) { const randomValue = ~~(Math.random() * 100); map.set(i, randomValue); array.push({id: i, randomValue}); obj[i] = randomValue; }
Tests:
map
for (i = 0; i < count; i++) { a = map.get(i); }
array
for (i = 0; i < count; i++) { a = array.find(el => el.id = i); }
obj
for (i = 0; i < count; i++) { a = obj[i] }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
map
array
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 dive into the provided benchmark JSON and explore what's being tested. **Benchmark Definition** The benchmark is designed to compare the performance of three data structures: `Map`, `Array`, and `Object`. The script preparation code creates: 1. A new `Map` instance (`map`) with 100,000 entries. 2. An empty `Array` instance (`array`) with 100,000 elements. 3. An empty `Object` instance (`obj`) with 100,000 properties. The loop iterates 100,000 times, and in each iteration: * For the "map" test case, it retrieves a value from the map using the `get()` method. * For the "array" test case, it finds an element in the array using the `find()` method with a callback function that checks if the element's `id` property matches the current index (`i`). * For the "obj" test case, it accesses a value from the object using the bracket notation (`obj[i]`). **Options being compared** The three options being compared are: 1. **Map**: A data structure that stores key-value pairs in an ordered collection of key-value pairs. 2. **Array**: A linear collection of values that can be accessed by index. 3. **Object**: An unordered collection of key-value pairs. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: 1. **Map**: * Pros: Efficient lookups, fast iteration, and good memory usage for large datasets. * Cons: May have higher overhead due to the use of a hash table, and not as intuitive to work with as other data structures. 2. **Array**: * Pros: Fast iteration, simple indexing, and widely supported. * Cons: Less efficient lookups compared to `Map`, especially for large datasets or sparse data. 3. **Object**: * Pros: Simple to access properties using bracket notation, and intuitive to work with. * Cons: Less efficient than `Map` for large datasets, as it uses a hash table under the hood. **Library usage** None of the benchmark cases use any external libraries or dependencies. **Special JS features or syntax** The benchmark makes use of some JavaScript features: 1. **Array.prototype.find()**: A method introduced in ECMAScript 2015 (ES6) that finds the first element in an array that satisfies a provided testing function. 2. **Map.prototype.get()**: A method introduced in ECMAScript 2015 (ES6) that retrieves the value associated with a given key from a map. **Other alternatives** If you'd like to explore alternative data structures or approaches, here are some options: 1. **Set**: Similar to `Map`, but without keys. 2. **SortedArray**: A custom implementation of an array that maintains its elements in sorted order. 3. **HashTable**: An external library or a built-in object (e.g., in some languages) that provides fast lookups and insertion/deletion operations. Keep in mind that each data structure has its strengths and weaknesses, and the best choice depends on the specific use case and performance requirements.
Related benchmarks:
Map vs Array vs Object set uint32 key speed
Map vs Array vs Object has uint32 key speed
map vs obj vs array 2
Array vs Class
Comments
Confirm delete:
Do you really want to delete benchmark?