Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
contains misses: Map vs Set vs Array vs Symbol vs UID
(version: 0)
Comparing performance of:
map vs set vs arr vs symbol vs uid vs control
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var map = new Map(), set = new Set(), arr = [], sym = Symbol(), uid = "some-uid", item = {}; // not in any // Create for (var i = 0; i < 5000; ++i) { var obj = {}; map.set(obj, true); set.add(obj); arr.push(obj); obj[sym] = true; obj[uid] = true; } function mapHas(thing) { return map.has(thing); } function setHas(thing) { return set.has(thing); } function arrHas(thing) { return arr.indexOf(thing) >= 0; } function symHas(thing) { return thing[sym]; } function uidHas(thing) { return thing[uid]; } function conHas(thing) { return false; }
Tests:
map
mapHas(item);
set
setHas(item);
arr
arrHas(item);
symbol
symHas(item);
uid
uidHas(item);
control
conHas(item);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
map
set
arr
symbol
uid
control
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):
**Overview of the Benchmark** The provided benchmark measures the performance of various data structures in JavaScript: `Map`, `Set`, `Array`, and `Symbol`. The test creates an object with unique identifiers (`UID`) and iterates over 5,000 objects to populate each data structure. The benchmark then runs a series of functions that check if a specific object exists within each data structure. **Data Structures Compared** 1. **Map**: A JavaScript implementation of a hash map, which stores key-value pairs. 2. **Set**: A JavaScript implementation of a set, which stores unique values. 3. **Array**: A JavaScript array, which is used to store sequences of values. 4. **Symbol**: A primitive value that represents a unique symbol in the language. **Options Compared** * **Map vs Set**: Both data structures are hash-based and can be used for fast lookups. However, `Set` only stores unique values, while `Map` allows key-value pairs. + Pros of `Map`: Can store additional information with each key-value pair, which might be useful in certain scenarios. + Cons of `Map`: More memory-intensive due to the storage of keys and values. * **Map vs Array**: Both data structures are used for storing sequences of values. However, `Array` is fixed-size, while `Map` can grow dynamically. + Pros of `Array`: Fixed-size, which might be beneficial in terms of memory usage and performance for certain use cases. + Cons of `Array`: Does not support key-value pairs or fast lookups by value. * **Set vs Array**: Both data structures are used for storing unique values. However, `Set` is designed for fast membership testing, while `Array` requires explicit checks. + Pros of `Set`: Fast membership testing and automatic removal of duplicates. + Cons of `Set`: Does not support indexing or random access. **Library Used** The benchmark uses the built-in `Map`, `Set`, `Array`, and `Symbol` data structures in JavaScript. These are implemented in V8, the JavaScript engine used by Google Chrome. **Special JS Feature/Syntax** This benchmark does not use any special JavaScript features or syntax beyond what is necessary for the test case. **Other Alternatives** For those who may be interested in exploring alternative data structures: * **WeakMap**: A `Map` that only stores weak references to keys, which can help prevent memory leaks. * **WeakSet**: A `Set` that only stores weak references to values, which can help prevent memory leaks. * **Linked Hash Map**: An implementation of a hash map that uses linked lists for collision resolution, which can improve performance in certain scenarios. Keep in mind that the choice of data structure ultimately depends on the specific requirements and constraints of your use case.
Related benchmarks:
Array from() vs Map.keys()
Array from() vs Map.keys() vs Map.values() vs spread (fixed)
POJOs vs Immutable Maps
Symbol vs Object identity access speed
Comments
Confirm delete:
Do you really want to delete benchmark?