Socket
Socket
Sign inDemoInstall

xksuid

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xksuid

Extended K-Sortable UID for browser & NodeJS 16+


Version published
Maintainers
1
Created
Source

Extended K-Sortable UID

This is a browser-friendly implementation of ksuid in JavaScript. The implementation is extended to produce UIDs in descending order, which is beneficial in certain cases, e.g. when used as a key for KV Cloudflare worker storage entries that should be listed in descending order.

Binary keys generated in ascending order per reference KSUID implementation are 20 bytes long, whereas extended KSUID are 21 byte long and are always starting with 61, indicating that timestamp value is calculated as a difference between maximum possible value (max 32bit unsigned integer) and seconds since 14e8:

# KSUID - Ascending order
   4 bytes           16 bytes
 [ ][ ][ ][ ]  [ ][ ][ ]...[ ][ ][ ]
sec since 14e8        random bytes

# xKSUID - Descending order
1 byte   4 bytes           16 bytes
[ 61 ] [ ][ ][ ][ ]  [ ][ ][ ]...[ ][ ][ ]
 'z'   sec till MAX        random bytes

Text representation for KSUID is 27 characters long, and 28 characters long with xKSUID, starting with z:

# KSUID
24bxFgAT8RPOk01OWecxD8bcLKo

#xKSUID
zYS4GmVK9Hn2JHPota8SAOBQsPgR

Usage

API:

generate() // ASC KSUID
generate(true) // DESC xKSUID
generate(true,new Date('2022-01-01').getTime()) // DESC xKSUID with custom timestamp

Via CDN:

<script>
  import {generate} from 'https://cdn.jsdelivr.net/npm/xksuid@0.0.1/src/index.js'
</script>

Via NPM:

npm install xksuid

For browser:

import {generate} from 'xksuid'

For NodeJS ESM:

import {generate} from 'xksuid/src/index.node.mjs'

For NodeJS CommonJS:

const {generate} = await import('xksuid/src/index.node.mjs')

Benchmark

> xksuid@0.0.1 benchmark
> node benchmark.js

8 x Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz

Running 100000 ops

3535.918315887451 ms 35.359183158874515 ns/op 28281.196302155477 ops/sec

Keywords

FAQs

Package last updated on 03 Feb 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc