Map Or Similar

1.5.0 · maintenance · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates instantiation of `MapOrSimilar` and basic usage of its core methods: `set`, `get`, `has`, `size`, `forEach`, and `delete` with various key types.

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'));

view raw JSON →