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

mdbid

v1.0.0

Published

Generate unique identifiers like MongoDB does

Downloads

5,651

Readme

view on npm downloads per month node version build status test coverage license

mdbid

This package can be used to generate unique string identifiers in a very performant way. The algorithm is identical to the one that MongoDB uses to create its document identifiers.

Usage


const oid = require('mdbid')

oid() // '579b64ac12b43732a08531d8'

API

mdbid()string mdbid.machineId: 3-byte integer to use as machine identifier. mdbid.processId: 2-byte integer to use as process identifier. mdbid.sequence: Initial value of the sequence counter. It's a 3-byte integer. mdbid.version: The version string from package manifest.

Installation

With npm:

npm install mdbid

Benchmark

The benchmark measures elapsed time it takes to perform 1500000 calls. (Which means that lower values are better.)

Here are the results of similar packages:

bson: 1571ms
mdbid: 593ms
mongoid: 6153ms
objectid: 6154ms
mongoid-js: 396ms

Motivation

In many of my projects Redis is used as the primary database, which means that it's not just the caching layer of the system, but actual documents (users, sessions, accounts, etc.) are stored in it.

Thus I need identifiers to include in Redis keys, but Redis does not provide any built-in solution for id generation.

It is true that you can create incremental numeric ids easily with atomic counters, but those ids are only identical per schema and a user can predict new ids. And that's unacceptable in a lot of real-life situations.

Mongo's id model seems like a good choice, because ids are guaranteed to be unique per server per process up to a really huge (2^(5*8)) cluster of servers (with proper configuration). So I decided to use it.

There are some already existing packages out there with the same purpose but some of them

  • are inefficient when the expected output is string
  • are not identical to Mongo's object id specification (last three bytes of an id must be a "counter, starting with a random value")
  • are not well-tested despite they're providing an unnecessarily complex API.

mongoid-js seemed like the best approach, but

  • it's code is unmaintained and messy
  • it not follows Node's code-style standards
  • the use of timers makes it not truly sync and counting calls can have unintended side effects.

So I decided to re-implement it in a sustainable manner. That's the story of mdbid.

License

MIT