An simple alternative to 2D arrays, with comparative performance.
Memory Efficient
- Built on a 1d array; Uses a single contiguous block of memory
- Fewer object allocations = less garbage collection overhead
- Better memory layout for very large maps
Cleaner API and Type Safety
- Single generic type:
FastMap<T>vsT[][] - No need to manage nested array initialization
- Consistent bounds checking across all operations (with safe Get/Set methods)
- Single generic type:
Let's be honest: If you're just storing simple data and don't need safety guarantees, a 2D array is simpler.
FastMap is best used for:
- Game dev (tile maps, grids where safety + performance matter)
- Image processing (pixel buffers)
- Large grid simulations
- Other cases where you want built-in bounds checking without manual if statements
npm install "@speakingsoftware/fastmap"
import{FastMap}from'@speakingsoftware/fastmap';interfaceRGB{r: number;g: number;b: number;}constimage=newFastMap<RGB>(100,100);// Draw a simple gradientfor(lety=0;y<100;y++){for(letx=0;x<100;x++){image.Set(x,y,{r: Math.floor(255*(x/100)),g: Math.floor(255*(y/100)),b: 128});}}Inspired by the CImage class in Vladimir Kovalevsky's book "Modern Algorithms for Image Processing"