uuid-timestamp

1.1.0 • Public • Published

uuid-timestamp

UUID v4 based on timestamp (non-standard algorithm)

  • Valid UUID version 4
  • Built in timestamp in nanoseconds
  • Absolute uniqueness within your ecosystem
  • Super easy and fast

Install

$ npm install uuid-timestamp

Usage

Emitting UUID

Emit valid UUID v4 with nanosecond timestamp

const { uuidEmit } = require('uuid-timestamp');
const uuid = uuidEmit();
console.log(`Your new UUID v4 is: ${uuid}`);
 
// Your new UUID v4 is: 15972459-4799-4612-a723-231092612723

Parsing UUID timestamp

Parse UUID and get it's timestamp in milliseconds or nanoseconds

const { uuidParse, uuidParseNano } = require('uuid-timestamp');
const timestamp = uuidParse(uuid);
console.log(timestamp);
console.log(`UUID created at ${new Date(timestamp).toUTCString()}`);
 
// 1597245947996
// UUID created at Wed, 12 Aug 2020 15:25:47 GMT
 
console.log(uuidParseNano(uuid));
 
// 1597245947996127232n

Get current nanosecond timestamp

Additional method to get current nanosecond timestamp from system time and process.hrtime.bigint() diff

const { nanoTime } = require('uuid-timestamp');
console.log(nanoTime());
 
// 1597246608194080627n

API

uuidEmit()

returns

Type: String
The function returns a valid UUID v4.

uuidParse(uuid)

uuid

Type: String
Previously generated UUID.

returns

Type: Number
The function returns a timestamp in milliseconds, parsed from UUID.

uuidParseNano(uuid)

uuid

Type: String
Previously generated UUID.

returns

Type: BigInt
The function returns a timestamp in nanoseconds, parsed from UUID.

nanoTime()

returns

Type: BigInt
The function returns current nanosecond timestamp from system time and process.hrtime.bigint() diff.

Benchmarks

A comparative performance test based on discussion thread on stackoverflow. You can run the test and see the results on jsben.ch (browser version uses millisecond timestamp instead of the nanosecond Node.js version, so there may be some performance fluctuations)

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.1.0
    1,088
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.1.0
    1,088
  • 1.0.6
    0
  • 1.0.5
    0
  • 1.0.4
    0
  • 1.0.3
    0

Package Sidebar

Install

npm i uuid-timestamp

Weekly Downloads

1,088

Version

1.1.0

License

ISC

Unpacked Size

4.26 kB

Total Files

3

Last publish

Collaborators

  • tarkh