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
$ npm install uuid-timestamp
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
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
Additional method to get current nanosecond timestamp from system time and process.hrtime.bigint()
diff
const { nanoTime } = require('uuid-timestamp');
console.log(nanoTime());
// 1597246608194080627n
Type: String
The function returns a valid UUID v4.
Type: String
Previously generated UUID.
Type: Number
The function returns a timestamp in milliseconds, parsed from UUID.
Type: String
Previously generated UUID.
Type: BigInt
The function returns a timestamp in nanoseconds, parsed from UUID.
Type: BigInt
The function returns current nanosecond timestamp from system time and process.hrtime.bigint()
diff.
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)