|
1 | 1 | # FastMap |
2 | 2 |
|
3 | | -A simple 2D map that is optimized for performance. |
| 3 | +An simple alternative to 2D arrays, with comparative performance. |
| 4 | + |
| 5 | +## Benefits: |
| 6 | + |
| 7 | +- Memory Efficient |
| 8 | + - Built on a 1d array; Uses a single contiguous block of memory |
| 9 | + - Fewer object allocations = less garbage collection overhead |
| 10 | + - Better memory layout for very large maps |
| 11 | + |
| 12 | +- Cleaner API and Type Safety |
| 13 | + - Single generic type: `FastMap<T>` vs `T[][]` |
| 14 | + - No need to manage nested array initialization |
| 15 | + - Consistent bounds checking across all operations (with safe Get/Set methods) |
| 16 | + |
| 17 | +## Usage: |
| 18 | + |
| 19 | +Let's be honest: If you're just storing simple data and don't need safety guarantees, _a 2D array is simpler_. |
| 20 | + |
| 21 | +FastMap is best used for: |
| 22 | +- Game dev (tile maps, grids where safety + performance matter) |
| 23 | +- Image processing (pixel buffers) |
| 24 | +- Large grid simulations |
| 25 | +- Other cases where you want built-in bounds checking without manual if statements |
| 26 | + |
| 27 | +## How to use: |
| 28 | +`npm install "@speakingsoftware/fastmap"` |
| 29 | + |
| 30 | +``` typescript |
| 31 | +import { FastMap } from '@speakingsoftware/fastmap'; |
| 32 | + |
| 33 | +interface RGB { |
| 34 | + r: number; |
| 35 | + g: number; |
| 36 | + b: number; |
| 37 | +} |
| 38 | + |
| 39 | +const image = new FastMap<RGB>(100, 100); |
| 40 | + |
| 41 | +// Draw a simple gradient |
| 42 | +for (let y = 0; y < 100; y++) { |
| 43 | + for (let x = 0; x < 100; x++) { |
| 44 | + image.Set(x, y, { |
| 45 | + r: Math.floor(255 * (x / 100)), |
| 46 | + g: Math.floor(255 * (y / 100)), |
| 47 | + b: 128 |
| 48 | + }); |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +---------- |
| 54 | + |
| 55 | +Inspired by the CImage class in Vladimir Kovalevsky's book "Modern Algorithms for Image Processing" |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + |
4 | 60 |
|
5 | | - * Internally, it is a 1D array that is accessed using 2D coordinates. This makes is extremely performant for larger maps, as it avoids the overhead and mess of nested arrays. |
6 | | - * Inspired by the CImage class in Vladimir Kovalevsky's book "Modern Algorithms for Image Processing" |
|
0 commit comments