Code Monkey home page Code Monkey logo

hyperid's Introduction

hyperid

Build Status

Uber-fast unique id generation, for Node.js and the browser. Here are the benchmarks:

crypto.randomUUID x 12,969,725 ops/sec ±0.88% (91 runs sampled)
hashids process.hrtime x 419,350 ops/sec ±0.66% (94 runs sampled)
hashids counter x 819,049 ops/sec ±0.58% (93 runs sampled)
shortid x 40,820 ops/sec ±2.49% (87 runs sampled)
crypto.random x 372,773 ops/sec ±2.39% (84 runs sampled)
nid x 1,614,450 ops/sec ±0.38% (93 runs sampled)
uuid.v4 x 1,446,051 ops/sec ±0.60% (98 runs sampled)
napiRsUuid.v4 x 8,676,151 ops/sec ±0.49% (97 runs sampled)
uuid.v1 x 2,051,072 ops/sec ±0.15% (99 runs sampled)
nanoid x 4,293,733 ops/sec ±0.31% (97 runs sampled)
hyperid - variable length x 25,937,129 ops/sec ±1.48% (91 runs sampled)
hyperid - fixed length x 24,970,478 ops/sec ±1.48% (92 runs sampled)
hyperid - fixed length, url safe x 25,856,735 ops/sec ±1.93% (92 runs sampled)

Fastest is hyperid - variable length,hyperid - fixed length, url safe
Slowest is shortid

Note: Benchmark run with Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz and Node.js v22.0.0

As you can see the native crypto.randomUUID is almost as fast as hyperid on Node.js v16, but not on v14.

Install

npm i hyperid --save

Example

'use strict'

const hyperid = require('hyperid')
const instance = hyperid()

const id = instance()

console.log(id)
console.log(instance())
console.log(hyperid.decode(id))
console.log(hyperid.decode(instance()))

API

hyperid([fixedLength || options])

Returns a function to generate unique ids. The function can accept one of the following parameters:

  • fixedLength: Boolean If fixedLength is true the function will always generate an id that is 33 characters in length, by default fixedLength is false.
  • options: Object If { fixedLength: true } is passed in, the function will always generate an id that is 33 characters in length, by default fixedLength is false. If { urlSafe: true } is passed in, the function will generate url safe ids according to RFC4648. If { startFrom: <int> } is passed in, the first counter will start from that number, which must be between 0 and 2147483647. Fractions are discarded, only the integer part matters.

instance()

Returns an unique id.

instance.uuid

The uuid used to generate the ids, it will change over time. It is regenerated every Math.pow(2, 31) - 1 to keep the integer a SMI (a V8 optimization).

hyperid.decode(id, [options])

Decode the unique id into its two components, a uuid and a counter. If you are generating url safe ids, you must pass { urlSafe: true } as option. It returns:

{
  uuid: '049b7020-c787-41bf-a1d2-a97612c11418',
  count: 1
}

This is aliased as instance.decode.

License

MIT

hyperid's People

Contributors

cadienvan avatar capaj avatar csarnataro avatar cwhittl avatar delvedor avatar dimfeld avatar dlion avatar jclaessens97 avatar mcollina avatar michaelrommel avatar osher avatar panmona avatar powerpaul17 avatar skellla avatar woss avatar yonathan06 avatar zekth avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hyperid's Issues

Non-Issue about code algorithm

I was looking at the code in hyperid.js and and came across:

function pad (count) {
  if (count < 10) return '000000000' + count
  if (count < 100) return '00000000' + count
  if (count < 1000) return '0000000' + count
  if (count < 10000) return '000000' + count
  if (count < 100000) return '00000' + count
  if (count < 1000000) return '0000' + count
  if (count < 10000000) return '000' + count
  if (count < 100000000) return '00' + count
  if (count < 1000000000) return '0' + count
  return count
}

A different algorithm that you might benchmark to see whether it yields any improvements:

function pad (count) {
  return '0'.repeat( Math.max( 0, 9 - Math.trunc( Math.log10(count) ) ) ) + count
}

could be that the unravelling in the original is faster.

import hyperid failed on vite v5.0.11

Error

hyperid.js?v=6295a50f:619 Uncaught TypeError: Cannot read properties of undefined (reading 'from')
    at node_modules/hyperid/hyperid.js (hyperid.js?v=6295a50f:619:33)
    at __require (hyperid.js?v=6295a50f:9:50)
    at hyperid.js?v=6295a50f:693:16

Repro

https://stackblitz.com/edit/vitejs-vite-jz2rfb?file=package.json,src%2Fmain.ts&terminal=dev

System

System:
    OS: macOS 14.2.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 11.21 GB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.11.0 - ~/.asdf/installs/nodejs/20.11.0/bin/node
    npm: 10.2.4 - ~/.asdf/plugins/nodejs/shims/npm
    pnpm: 8.14.1 - ~/Library/pnpm/pnpm
  Browsers:
    Chrome: 120.0.6099.234
    Safari: 17.2.1

Doesn't work with react-native

Hey, this library works nice in node/browser. But unfortunately not with react-native, since there is no global Buffer element. Replacing that part with require('buffer').Buffer works.

So maybe this could be changed to something along: let buffer = typeof Buffer !== 'undefined' ? Buffer : require('buffer').Buffer;?

urlSafe swaps +- and /_ encoding as per RFC 4648

Hi Matteo,

in line 70

return base64Id.replace(/\+/g, '_').replace(/\//g, '-')
the urlSafe option triggers a replacement of + with _ and of / with -.

Per RFC4648 (https://datatracker.ietf.org/doc/html/rfc4648#page-8) it should be exactly opposite. Since nobody knows, how the users of hyperid use the resulting id and if they are depending on exactly this conversion, it might be a breaking change if you wanted the RFC behaviour.

Since I do not know, if the swap was intentional or not, I have not (yet) created a PR. It is not an issue for me, as I am about to swap out hyperid with a combination of standard crypto.randomUUID plus a nanoid, just because for starters I do not need the extreme raw performance and security folks do not want an enumerable ID...

But I - of course - will create a PR, if you'd like. After all, it is just a two-line change (decode function is affected, too). I could not find test cases which would need adaptation.

Cheers,

Michael.

import hyperid failed on [email protected]

loadable.esm.js:377 Uncaught TypeError: uuidv4 is not a function
at hyperid (hyperid.js:19:19)
at uuidGenerator (uuid.tsx:7:10)
at uuid.tsx:13:16

Reproduction
https://stackblitz.com/edit/vitejs-vite-oruu6k?file=package.json,main.js,counter.js&terminal=dev

system info:
System:
OS: macOS 12.6
CPU: (4) x64 Intel(R) Core(TM) i7-7567U CPU @ 3.50GHz
Memory: 26.68 MB / 16.00 GB
Shell: 5.8.1 - /bin/zsh
Binaries:
Node: 18.12.0 - ~/.nvm/versions/node/v18.12.0/bin/node
Yarn: 1.22.15 - ~/.yarn/bin/yarn
npm: 8.19.2 - ~/.nvm/versions/node/v18.12.0/bin/npm
Browsers:
Chrome: 107.0.5304.87
Firefox: 106.0.3
Safari: 16.0

reference issue:
vitejs/vite#10810

getting stuck at "/undefined" as part of id...

I ran this simple experiment:

> id = require('hyperid')({ fixedLength: true })
[Function: generate] {
  uuid: '3ec503a9-6dd2-4f53-b85f-380d08ccda74',
  decode: [Function: decode]
}
> i = 0; while (++i < 100000000000 ) { 0 == i % 1000000 ?  console.log( id() )  : id () }
apJUntnyRUagA934HnZreg/0000999999
apJUntnyRUagA934HnZreg/0001999999
apJUntnyRUagA934HnZreg/0002999999
apJUntnyRUagA934HnZreg/0003999999
apJUntnyRUagA934HnZreg/0004999999
apJUntnyRUagA934HnZreg/0005999999
...

which ended sadly with:

apJUntnyRUagA934HnZreg/0993999999
apJUntnyRUagA934HnZreg/0994999999
apJUntnyRUagA934HnZreg/0995999999
apJUntnyRUagA934HnZreg/0996999999
apJUntnyRUagA934HnZreg/0997999999
apJUntnyRUagA934HnZreg/0998999999
apJUntnyRUagA934HnZreg/0999999999
apJUntnyRUagA934HnZreg/undefined
apJUntnyRUagA934HnZreg/undefined
apJUntnyRUagA934HnZreg/undefined
apJUntnyRUagA934HnZreg/undefined
apJUntnyRUagA934HnZreg/undefined
^CUncaught Error: Script execution was interrupted by `SIGINT`

woups?

ubuntu, node 12.

bump uuid version

Slonik uses your package and I got the following warning when installing it:
hyperid > [email protected]: Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.
Can & should we bump the version of the uuid package used here?

The v2.3.0 version dose not work

The v2.3.0 version does not work due to the missing uuid.js file.

It will throw the error below when running:

Error: Cannot find module './uuid'
Require Stack:
-  ....
-  ...
-  ...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.