GithubHelp home page GithubHelp logo

richcontext / hyperid Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cwhittl/hyperid

0.0 1.0 0.0 88 KB

Uber-fast unique id generation, for Node.js and the browser

License: MIT License

JavaScript 95.00% TypeScript 5.00%

hyperid's Introduction

hyperid

Build Status

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

hashids process.hrtime x 161,779 ops/sec ±3.25% (78 runs sampled)
hashids counter x 327,099 ops/sec ±2.45% (76 runs sampled)
shortid x 26,225 ops/sec ±3.90% (72 runs sampled)
nid x 864,834 ops/sec ±2.94% (83 runs sampled)
uuid.v4 x 269,960 ops/sec ±2.42% (81 runs sampled)
uuid.v1 x 1,687,472 ops/sec ±1.38% (85 runs sampled)
hyperid - variable length x 6,121,953 ops/sec ±2.66% (76 runs sampled)
hyperid - fixed length x 6,331,137 ops/sec ±2.31% (79 runs sampled)

Note: Benchmark run with 1,3 GHz Intel Core i5 using Node v8.10.0

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.

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

03k64 avatar cwhittl avatar delvedor avatar dimfeld avatar dlion avatar mcollina avatar

Watchers

 avatar

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.