Appearance
PriceData Map-Based Storage
Performance optimization converting PriceData from plain objects to Map.
Benchmark Results (26 entries - asset prices)
| Operation | Object | Map | Speedup |
|---|---|---|---|
| Creation | 7.25μs | 2.02μs | 3.6x |
| Iteration | 1.05μs | 0.075μs | 14x |
| Merge | 8.75μs | 1.89μs | 4.6x |
| Has-check | 0.89μs | 0.20μs | 4.4x |
For cache size checks (30k entries):
Object.keys().length: 5.4s (O(n))Map.size: 0.022ms (O(1))- 245,000x speedup
Type Definitions
typescript
// Internal storage (fast)
type PriceData = Map<SymbolIdentifiers, number>;
// JSON serialization (RPC boundaries)
type PriceDataObject = { [ticker in SymbolIdentifiers]?: number };Helper Functions
typescript
import {
createPriceData, // Create empty Map
priceDataFromObject, // Object → Map
priceDataToObject, // Map → Object (RPC)
mergePriceData, // Merge multiple Maps
clonePriceData, // Clone a Map
} from "@/constants/price-data";Usage Patterns
typescript
// Internal: Use Map methods
const prices = createPriceData();
prices.set(SymbolIdentifiers.BTCUSD, 50000);
const btcPrice = prices.get(SymbolIdentifiers.BTCUSD);
const hasETH = prices.has(SymbolIdentifiers.ETHUSD);
const count = prices.size; // O(1)!
// RPC: Convert at boundaries
return priceDataToObject(prices);Architecture
Internal Storage RPC Boundary External
──────────────────────────────────────────────────────
Map<Symbol, number> → priceDataToObject() → JSON
← priceDataFromObject() ←- Internal:
Mapfor performance - RPC returns: Plain objects (JSON-serializable)
- Conversion: Only at boundaries, minimal overhead