npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mapject

v1.0.0

Published

Get the performance of a Map with the syntax of an object.

Downloads

4

Readme

Mapject

Get the performance of a Map with the syntax of an object.

CI status

Why?

Objects in JavaScript use a natural syntax for key-value storage, but unlike Maps, they aren't optimized for large or dynamic collections. See other differences here.

Often, objects are a good fit for simple key-value applications, but need to be refactored to Maps later. But refactoring objects to Maps can be a headache, especially when using a type system. For example, this refactor changes every single line and changes the object's type:

- let obj: Record<string, number> = {
-   asdf: 1234,
-   ghjk: 5678,
- };
- obj.zxcv = 890;
- delete obj.ghjk;
+ let obj: Map<string, number> = new Map([
+   ["asdf", 1234],
+   ["ghjk", 5678],
+ ]);
+ obj.set("zxcv", 890);
+ obj.delete("ghjk");

Mapject lets you do the same refactor with this:

- let obj: Record<string, number> = {
+ let obj: Record<string, number> = Mapject({
    asdf: 1234,
    ghjk: 5678,
- };
+ });
  obj.zxcv = 890;
  delete obj.ghjk;

Install

$ npm install mapject
$ yarn add mapject

Usage

Use a Mapject exactly like you would use an object.

// Create a Mapject
let mj = Mapject();

// Create a Mapject with an underlying Map
// - mj proxies the passed map, so the map will reflect all changes
let mj = Mapject(new Map([["asdf", 1234]]));

// Create a Mapject with initial values from an object
// - the object will not reflect changes
let mj = Mapject({ asdf: 1234, ghjk: 5678 });

// Getting and setting
mj.asdf = 1234;
console.log(mj.asdf); // 1234

// Checking if Mapject has key
("asdf" in mj) // true

// Object.* methods
Object.defineProperty(mj, "asdf", { value: 1234 });

// Copy to plain object
const obj = {...mj};

// Iterate over [key, value] pairs
for (let [key, value] of mj) { ... }
let keyValuePairs = [...mj];

// Get number of keys in the mapject
Mapject.size(mj);

// Get the underlying map
Mapject.getMap(mj); // Map

Notes

Similar to Object.create(null), a Mapject instance doesn't inherit methods from Object.prototype. No special meaning is given to properties like "prototype", "constructor", "__proto__", or "hasOwnProperty", so there is no danger to setting user-supplied properties on a Mapject.

(mj.__proto__) // undefined
mj.__proto__ = "anything";

To use methods from Object.prototype, use Function#call:

Object.prototype.hasOwnProperty.call(mj, "asdf");

Mapject requires ES6 Proxy to work.