UNPKG

hytopia

Version:

The HYTOPIA SDK makes it easy for developers to create massively multiplayer games using JavaScript or TypeScript.

280 lines (130 loc) 4.34 kB
<!-- Do not edit this file. It is automatically generated by API Documenter. --> [Home](./index.md) &gt; [server](./server.md) &gt; [IterationMap](./server.iterationmap.md) ## IterationMap class A high-performance Map-like data structure optimized for frequent iteration. When to use: per-tick collections that are built, iterated, and cleared each frame. Do NOT use for: long-lived maps with rare iteration; a standard Map is simpler. **Signature:** ```typescript export default class IterationMap<K, V> ``` ## Remarks IterationMap maintains both a Map for O(1) lookups and an Array for fast iteration, eliminating the need for Array.from() calls and providing \~2x faster iteration than Map.values(). Optimized for "build up, iterate, clear" usage patterns common in game loops. Pattern: update via `IterationMap.set`<!-- -->, iterate with `IterationMap.valuesArray`<!-- -->, then `IterationMap.clear`<!-- -->. Anti-pattern: mutating the map during `IterationMap.valuesArray` iteration. ## Example ```typescript const iterationMap = new IterationMap<number, string>(); iterationMap.set(1, 'hello'); iterationMap.set(2, 'world'); // Fast O(1) lookup const value = iterationMap.get(1); // Fast array iteration (no Map.values() overhead) for (const item of iterationMap.valuesArray) { console.log(item); } // Efficient bulk clear iterationMap.clear(); ``` \*\*Category:\*\* Utilities ## Properties <table><thead><tr><th> Property </th><th> Modifiers </th><th> Type </th><th> Description </th></tr></thead> <tbody><tr><td> [size](./server.iterationmap.size.md) </td><td> `readonly` </td><td> number </td><td> Returns the number of key-value pairs in the IterationMap. \*\*Category:\*\* Utilities </td></tr> <tr><td> [valuesArray](./server.iterationmap.valuesarray.md) </td><td> `readonly` </td><td> readonly V\[\] </td><td> Returns a readonly array of all values for fast iteration. This is the key performance feature - use this instead of .values() for iteration. \*\*Side effects:\*\* Rebuilds the backing array when the map has changed. \*\*Category:\*\* Utilities </td></tr> </tbody></table> ## Methods <table><thead><tr><th> Method </th><th> Modifiers </th><th> Description </th></tr></thead> <tbody><tr><td> [\[Symbol.iterator\]()](./server.iterationmap._symbol.iterator_.md) </td><td> </td><td> Returns an iterator for the key-value pairs in the IterationMap. </td></tr> <tr><td> [clear()](./server.iterationmap.clear.md) </td><td> </td><td> Removes all key-value pairs from the IterationMap. Highly optimized for the common "build up, iterate, clear" pattern. \*\*Side effects:\*\* Clears the backing map and value array. \*\*Category:\*\* Utilities </td></tr> <tr><td> [delete(key)](./server.iterationmap.delete.md) </td><td> </td><td> Removes the key-value pair from the IterationMap. </td></tr> <tr><td> [entries()](./server.iterationmap.entries.md) </td><td> </td><td> Returns an iterator for the key-value pairs in the IterationMap. </td></tr> <tr><td> [forEach(callbackfn, thisArg)](./server.iterationmap.foreach.md) </td><td> </td><td> Executes a provided function once for each key-value pair. </td></tr> <tr><td> [get(key)](./server.iterationmap.get.md) </td><td> </td><td> Returns the value associated with the key, or undefined if the key doesn't exist. </td></tr> <tr><td> [has(key)](./server.iterationmap.has.md) </td><td> </td><td> Returns true if the key exists in the IterationMap. </td></tr> <tr><td> [keys()](./server.iterationmap.keys.md) </td><td> </td><td> Returns an iterator for the keys in the IterationMap. </td></tr> <tr><td> [set(key, value)](./server.iterationmap.set.md) </td><td> </td><td> Sets the value for the key in the IterationMap. </td></tr> <tr><td> [values()](./server.iterationmap.values.md) </td><td> </td><td> Returns an iterator for the values in the IterationMap. Note: For performance-critical iteration, use .valuesArray instead. </td></tr> </tbody></table>