@ryanmorr/fastmap
v1.0.1
Published
Accelerated hash maps
Downloads
202
Maintainers
Readme
fastmap
Accelerated hash maps
Description
Creates an efficient key/value store by instantiating a constructor function with an empty prototype. This is faster than using Object.create(null)
to create "bare" objects, particularly in V8, making it the superior alternative for hash maps in memory intensive tasks. Full credit to Node.js for the technique.
Install
Download the CJS, ESM, UMD versions or install via NPM:
npm install @ryanmorr/fastmap
Usage
Use just like an object literal:
import fastmap from '@ryanmorr/fastmap';
const map = fastmap();
map.foo = 1;
map.bar = 2;
{}.toString.call(map); //=> "[object Object]"
JSON.stringify(map); //=> "{\"foo\":1,\"bar\":2}"
Unlike object literals, the object is empty:
'toString' in {}; //=> true
'toString' in fastmap(); //=> false
for (const key in map) {
// `hasOwnProperty` check is unnecessary
}
Provide an object to pre-populate the map:
const map = fastmap(foo: 1, bar: 2, baz: 3});
map.foo; //=> 1
map.bar; //=> 2
map.baz; //=> 3
License
This project is dedicated to the public domain as described by the Unlicense.