|
| 1 | +# Choosing the Right Collection |
| 2 | + |
| 3 | +Selecting the appropriate collection type for your NEAR smart contract is crucial for both functionality and gas efficiency. This guide will help you understand the tradeoffs between different collection types. |
| 4 | + |
| 5 | +## Collection Types Overview |
| 6 | + |
| 7 | +| Collection | Iterable? | Ordered? | Unique Keys? | Memory Overhead | Use Case | |
| 8 | +|------------|-----------|----------|--------------|----------------|----------| |
| 9 | +| `Vector` | ✅ | ✅ (by insertion) | ❌ | Low | Ordered list of items | |
| 10 | +| `LookupMap` | ❌ | ❌ | ✅ | Lowest | Fast key-value lookups | |
| 11 | +| `UnorderedMap` | ✅ | ❌ | ✅ | Medium | Key-value with iteration | |
| 12 | +| `LookupSet` | ❌ | ❌ | ✅ | Lowest | Fast unique value lookups | |
| 13 | +| `UnorderedSet` | ✅ | ❌ | ✅ | Medium | Unique values with iteration | |
| 14 | +| `TreeMap` | ✅ | ✅ (by key) | ✅ | Highest | Ordered key-value pairs | |
| 15 | + |
| 16 | +## Decision Flowchart |
| 17 | + |
| 18 | +``` |
| 19 | +Do you need to store key-value pairs? |
| 20 | +├── Yes → Do you need to iterate over all entries? |
| 21 | +│ ├── Yes → Do you need keys to be ordered? |
| 22 | +│ │ ├── Yes → Use TreeMap |
| 23 | +│ │ └── No → Use UnorderedMap |
| 24 | +│ └── No → Use LookupMap |
| 25 | +└── No → Do you need to store unique values? |
| 26 | + ├── Yes → Do you need to iterate over all values? |
| 27 | + │ ├── Yes → Use UnorderedSet |
| 28 | + │ └── No → Use LookupSet |
| 29 | + └── No → Do you need indexed access or ordered items? |
| 30 | + ├── Yes → Use Vector |
| 31 | + └── No → Use Vector (still the best choice) |
| 32 | +``` |
| 33 | + |
| 34 | +## When to Use Each Collection |
| 35 | + |
| 36 | +### Use `Vector` when you need: |
| 37 | + |
| 38 | +- An ordered list of items |
| 39 | +- Index-based access (e.g., `my_vector[5]`) |
| 40 | +- To frequently add items to the end of a list |
| 41 | +- To maintain insertion order |
| 42 | + |
| 43 | +### Use `LookupMap` when you need: |
| 44 | + |
| 45 | +- Fast key-value lookups |
| 46 | +- Gas efficiency (lowest overhead) |
| 47 | +- Only key-based access, never iteration |
| 48 | +- To check if a key exists |
| 49 | + |
| 50 | +### Use `UnorderedMap` when you need: |
| 51 | + |
| 52 | +- Both key-value lookups and iteration |
| 53 | +- To get all keys, values, or key-value pairs |
| 54 | +- A classic dictionary/map with full functionality |
| 55 | + |
| 56 | +### Use `LookupSet` when you need: |
| 57 | + |
| 58 | +- To check if a value exists (membership) |
| 59 | +- Gas efficiency (lowest overhead) |
| 60 | +- Only membership testing, never iteration |
| 61 | + |
| 62 | +### Use `UnorderedSet` when you need: |
| 63 | + |
| 64 | +- Both membership testing and iteration |
| 65 | +- To get all unique values in the set |
| 66 | +- A classic set with full functionality |
| 67 | + |
| 68 | +### Use `TreeMap` when you need: |
| 69 | + |
| 70 | +- Keys to be kept in sorted order |
| 71 | +- Range queries (get all keys between X and Y) |
| 72 | +- To find the nearest key (floor/ceiling operations) |
| 73 | +- Ordered iteration through keys |
| 74 | + |
| 75 | +## Gas and Storage Considerations |
| 76 | + |
| 77 | +Collections have different storage and gas cost profiles: |
| 78 | + |
| 79 | +- **Non-iterable collections** (`LookupMap`, `LookupSet`) have the lowest storage overhead but cannot be iterated over |
| 80 | +- **Iterable collections** (`UnorderedMap`, `UnorderedSet`) require additional storage to track keys/values for iteration |
| 81 | +- **Ordered collections** (`Vector`, `TreeMap`) have additional overhead to maintain order |
| 82 | + |
| 83 | +## Collection Size Recommendations |
| 84 | + |
| 85 | +| Collection Size | Recommended Collection Type | |
| 86 | +|-----------------|---------------------------| |
| 87 | +| Small (< 100 items) | Any collection type is fine | |
| 88 | +| Medium (100-1,000 items) | Consider gas efficiency more carefully | |
| 89 | +| Large (> 1,000 items) | Use non-iterable collections when possible and implement pagination | |
| 90 | + |
| 91 | +## Examples |
| 92 | + |
| 93 | +### Tokens in an NFT Contract |
| 94 | + |
| 95 | +```python |
| 96 | +# Store a list of all token IDs |
| 97 | +token_ids = Vector("token_ids") |
| 98 | + |
| 99 | +# Store token metadata by token ID |
| 100 | +token_metadata = UnorderedMap("token_metadata") |
| 101 | + |
| 102 | +# Track which tokens are owned by which accounts |
| 103 | +tokens_by_owner = UnorderedMap("tokens_by_owner") |
| 104 | +``` |
| 105 | + |
| 106 | +### Voting System |
| 107 | + |
| 108 | +```python |
| 109 | +# Track valid voters |
| 110 | +eligible_voters = LookupSet("eligible_voters") |
| 111 | + |
| 112 | +# Track who has already voted |
| 113 | +voted = LookupSet("voted") |
| 114 | + |
| 115 | +# Store votes by proposal ID |
| 116 | +votes_by_proposal = UnorderedMap("votes_by_proposal") |
| 117 | + |
| 118 | +# Store proposals in order by closing time |
| 119 | +proposals_by_end_time = TreeMap("proposals_by_end_time") |
| 120 | +``` |
| 121 | + |
| 122 | +### User Profiles |
| 123 | + |
| 124 | +```python |
| 125 | +# Store user profiles by account ID |
| 126 | +profiles = LookupMap("profiles") |
| 127 | + |
| 128 | +# Track verified users |
| 129 | +verified_users = LookupSet("verified_users") |
| 130 | + |
| 131 | +# Featured profiles in priority order |
| 132 | +featured_profiles = Vector("featured_profiles") |
| 133 | +``` |
| 134 | + |
| 135 | +## Hybrid Approaches |
| 136 | + |
| 137 | +Sometimes you might need multiple collections to achieve your goals efficiently: |
| 138 | + |
| 139 | +```python |
| 140 | +# For a system needing both fast lookups and ordered access: |
| 141 | + |
| 142 | +# Fast lookup by ID |
| 143 | +items_by_id = LookupMap("items_by_id") |
| 144 | + |
| 145 | +# Ordered listing of IDs |
| 146 | +ordered_item_ids = Vector("ordered_item_ids") |
| 147 | + |
| 148 | +# Usage: |
| 149 | +def get_item(item_id): |
| 150 | + return items_by_id.get(item_id) |
| 151 | + |
| 152 | +def get_items_paginated(from_index, limit): |
| 153 | + # Get IDs in order |
| 154 | + ids = ordered_item_ids[from_index:from_index + limit] |
| 155 | + # Retrieve each item by ID |
| 156 | + return [items_by_id[id] for id in ids] |
| 157 | +``` |
| 158 | + |
| 159 | +## Summary |
| 160 | + |
| 161 | +1. For simple ordered lists, use `Vector` |
| 162 | +2. For key-value lookups without iteration, use `LookupMap` |
| 163 | +3. For key-value with iteration, use `UnorderedMap` |
| 164 | +4. For unique value checking without iteration, use `LookupSet` |
| 165 | +5. For unique values with iteration, use `UnorderedSet` |
| 166 | +6. For ordered key-value data, use `TreeMap` |
| 167 | + |
| 168 | +Choose the most restrictive collection that meets your needs to maximize gas efficiency. |
0 commit comments