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

maskerade

v0.0.1

Published

Bitmask helper library written in Typescript

Downloads

1

Readme

Maskerade

Maskerade is a TypeScript library with no runtime dependencies for easily interfacing with bitmasks - integers which store multiple values in specific bits.

Installation

$ npm install maskerade

Maskerade exports two functions: Bitmask and setAssertMode. The latter function allows you to disable runtime sanity checking for performance reasons - though it's recommended to leave assertions on, especially during development.

Why bitmasks?

Bitmasks are useful primarily for reducing memory usage. Consider, for example, an array representing a game map, where each tile can store two values: height and terrain type. Say you have 4 kinds of terrain and 16 height levels. A simple JS object representing this would look like the following:

{
  terrainType: 2,
  height: 12
}

But this could also be stored in a single 32-bit integer, like so:

        unused             | height | terrainType
00000000000000000000000000    1100        10

This method of storing data uses vastly less memory than true objects, but it's difficult to work with. Retrieving the data requires bit-fiddling which is often difficult to understand, hard to write, and totally unreadable if you're looking at the code months later.

Bitmasks are often faster than direct object access, but that requires doing the bit-shifting directly, without the indirection of method calls. In many cases, the memory benefits of bitmasks are useful but the unreadability and bugginess of frequent bitshifting make it unfeasible. Giving a sane interface to it allows you to reap the memory benefits with just a small latency overhead vs objects - in my initial testing, Maskerade is about 10% slower than direct object access (though you can use bitshifting with Maskerade, for instance in tight performance-sensitive loops), but only uses about 15% of the memory.

Creating bitmasks with Maskerade

Maskerade's interface is fairly simple. It provides a Bitmask class which is constructed like so:

import { Bitmask } from "maskerade";

// The dataSections should contain a set of objects with "name" and
// "length" properties. The name is used as a key when actually
// creating a bitmask. The length is the number of bits in the integer
// to use to store that piece of data - for 4 possible values we need
// 2 bits, for 16 we need 4, etc.
const dataSections = [
  { name: "terrainType", length: 2 },
  { name: "height",      length: 4 }
];

const exampleBitmask = new Bitmask(dataSections);

const val = exampleBitmask.create({
  "terrainType": 2,
  "height": 12
});

val // -> 50
Bitmask.Print(val) // -> "00000000000000000000000000110010"

You can also easily get and set sections of a value:

exampleBitmask.getSection(val, "terrainType") // -> 2

exampleBitmask.getSection(val, "height") // -> 12

const val2 = exampleBitmask.setSection(val, "height", 11);
exampleBitmask.getSection(val2, "height") // -> 11

You can also turn a value into a JS object:

exampleBitmask.toObject(val2)
// -> {
//   terrainType: 2,
//   height: 11
// }

Development

git clone [email protected]:Benaiah/maskerade.git
cd maskerade
npm install --dev                              # Install dev dependencies
npm test                                       # Run tests (at src/test.ts)
npm run build                                  # Compile JS and definitions