Map Or Similar
Map Or Similar is a JavaScript utility that provides a `Map`-like object, defaulting to the native `Map` if available in the environment, or falling back to a custom polyfill implementation otherwise. Currently at version 1.5.0, it focuses on delivering a high-performance, dependency-free solution for environments where native `Map` support might be lacking or incomplete. The library supports core `Map` methods such as `set`, `get`, `has`, `delete`, `forEach`, and the `size` property, making it suitable for basic key-value storage. It is designed to work seamlessly in both browser and Node.js environments. Its primary differentiator is its lightweight footprint and performance-oriented implementation, achieved by only replicating a subset of the full `Map` API, rather than attempting a complete polyfill.
Common errors
-
TypeError: myMap.keys is not a function
cause Attempting to use a Map method (like `keys()`, `values()`, `entries()`, `clear()`) that is not implemented by `map-or-similar`.fixReview the documentation and use only the supported methods: `set`, `get`, `has`, `delete`, `forEach`, and `size`. For iteration, rely on `forEach` instead of iterator methods. -
ReferenceError: MapOrSimilar is not defined
cause The `map-or-similar` module was not correctly imported or required, or the variable name `MapOrSimilar` was misspelled.fixEnsure you have `const MapOrSimilar = require('map-or-similar');` (CommonJS) or `import MapOrSimilar from 'map-or-similar';` (ESM, with bundler support) at the top of your file.
Warnings
- gotcha This library only implements a subset of the standard `Map` API. Methods like `keys()`, `values()`, `entries()`, `clear()`, and the `Symbol.iterator` are not supported. Attempting to use these will result in a `TypeError`.
- gotcha The library primarily targets CommonJS (CJS) environments as demonstrated by its `require()` examples. While modern bundlers might transpile ES Modules (ESM) `import` statements, direct ESM compatibility might vary or require specific build configurations.
- gotcha The package falls back to a polyfill only if `Map` is not available. In environments where `Map` *is* available but has specific quirks or performance issues you wish to avoid, this library will still return the native `Map` instance, not its own polyfill.
Install
-
npm install map-or-similar -
yarn add map-or-similar -
pnpm add map-or-similar
Imports
- MapOrSimilar
import MapOrSimilar from 'map-or-similar';
const MapOrSimilar = require('map-or-similar'); - MapOrSimilar
import { MapOrSimilar } from 'map-or-similar';import MapOrSimilar from 'map-or-similar';
- Usage
const myMap = MapOrSimilar();
const myMap = new MapOrSimilar();
Quickstart
const MapOrSimilar = require('map-or-similar');
// Create a new map-like object.
// This will either be a native Map if available, or a custom polyfill implementation.
const myMap = new MapOrSimilar();
// Set various types of keys and values
myMap.set('stringKey', 'stringValue');
myMap.set(123, { id: 1, name: 'Number Key Object' });
const complexKey = { type: 'object', id: 'complex' };
myMap.set(complexKey, 'value associated with complex object');
// Retrieve values
console.log('Value for stringKey:', myMap.get('stringKey'));
console.log('Value for complexKey:', myMap.get(complexKey));
// Check for existence and size
console.log('Has 123?', myMap.has(123));
console.log('Current size:', myMap.size);
// Iterate over entries
myMap.forEach(function(value, key, map) {
console.log(`Key: ${JSON.stringify(key)}, Value: ${JSON.stringify(value)}`);
});
// Delete an entry
myMap.delete('stringKey');
console.log('Size after deletion:', myMap.size);
console.log('Has stringKey?', myMap.has('stringKey'));