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

bitbob

v1.0.3

Published

A big collection of tiny bit hacks from your tiny bit buddy!

Downloads

213

Readme

bitbob

NPM license NPM version NPM downloads

Speed up your math with smart little bit hacks from your smart little bit buddy!

    pnpm i bitbob

More performance through bit manipulation!

Instead of using built-in Math functions that are build to be as flexible and broad as possible, bitbob functions only focus on 32-bit integer, i.e. native numbers without a floating point component. This, for example, ups the performance of working with arrays (array indices, array length, array index pointers, ...).

import { min } from 'bitbob'

export default function includes(a: string[], b: string[]): boolean {
    const aLen = a.length
    const bLen = b.length
    const len = min(aLen, bLen) // Faster than Math.min!
    for (let i = 0; i < len; i++) if (a[i] === b[i]) return true
    return false
}

Some bit hacks are so small that they are hard to use them as a function because of typical function overhead. Therefore, here is a compiled list of other tiny math optimizations through bit hacks you can do: List

Reduce your memory usage!

Data structures sometimes come with a very complex lifecycle to, for example, handle user input. Instead of creating dozens of boolean properties for each and every single state, we can compact everything to ONE number.


import { BitMap } from 'bitbob'

const enum States {
    Starting // 0b1
    Closing, // 0b10
    Scrolling // 0b100
    Zooming, // 0b1000
    Traversing // 0b10000
}

export default class Component {
    states = new BitMap()

    update() {
        if (this.states.isSet(States.Closing)) return cleanup()
        else if (this.states.isMet(States.Scrolling | States.Zooming)) { // = 12 = 1100
            this.move()
        } 
    }

    scroll() {
        this.states.set(States.Scrolling)
    }

    zoom() {
        this.states.set(States.Zooming)
    }

    close() {
        this.states.set(States.Closing)
    }
}

This is not everything. You can also store numbers inside numbers!


import { ComposedNumber } from 'bitbob'

function handleScreenRes(data: number) {
    const height = data & 0x3FF         // Width = 1020 AND 10 bit mask
    const width = data >> 10 & 0x7FF    // Height = 1980 AND 11 bit mask plus shift to right from previous number
    return //...
}

const cn = new ComposedNumber()
cn.set(1020)
cn.set(1980)

handleScreenRes(cn.state)

Resources

Bitbob is a collection of old bit hacks ported to javascript for faster math and faster algorithms. The following includes resources for the used algorithms.

https://graphics.stanford.edu/~seander/bithacks.html

https://github.com/virtyaluk/Bit-Twiddling-Hacks

https://ocw.mit.edu/courses/6-172-performance-engineering-of-software-systems-fall-2018/cc6983c9ebd77c28e8ae85bc0e575360_MIT6_172F18_lec3.pdf

https://brilliant.org/wiki/bit-manipulation-hacks/

https://github.com/mikolalysenko/double-bits

https://medium.com/@amirulislamalmamun/bitwise-hacks-21a884f80481


© Torathion 2024