3.2.81 • Published 5 months ago

@thi.ng/ksuid v3.2.81

Weekly downloads
1
License
Apache-2.0
Repository
github
Last release
5 months ago

ksuid

npm version npm downloads Twitter Follow

This project is part of the @thi.ng/umbrella monorepo.

About

Configurable K-sortable unique IDs, ULIDs, binary & base-N encoded, 32/48/64bit time resolutions.

Idea based on segmentio/ksuid, though the added flexibility in terms of configuration & implementation also enables the creation of ULIDs:

FeatureKSUID defaultULID default
Configurable bit size160 bits128 bits
Base-N encoding schemebase62(1)base32 (Crockford)
Timestamp resolutionseconds (32 bits)milliseconds (48 bits)
milliseconds (64 bits)
Epoch start time offsetapprox. 2020-09-13none
Time-only base ID generation
ID parsing / decomposition
Configurable RNG source(2)
  • (1) See @thi.ng/base-n for alternatives
  • (2) Default: window.crypto, Math.random as fallback

IDs generated w/ this package are composed of a 32, 48 or 64 bit Unix epochs (by default time shifted to free up bits for future timestamps) and N additional bits of a random payload (from a configurable source). IDs can be generated as byte arrays or base-N encoded strings. For the latter, the JS runtime MUST support BigInt.

KSUID bit layout diagram

Status

STABLE - used in production

Search or submit any issues for this package

Related packages

  • @thi.ng/base-n - Arbitrary base-n conversions w/ presets for base16/32/36/58/62/64/85, support for arrays & bigints
  • @thi.ng/idgen - Generator of opaque numeric identifiers with optional support for ID versioning and efficient re-use
  • @thi.ng/random - Pseudo-random number generators w/ unified API, distributions, weighted choices, ID generation

Installation

yarn add @thi.ng/ksuid

ES module import:

<script type="module" src="https://cdn.skypack.dev/@thi.ng/ksuid"></script>

Skypack documentation

For Node.js REPL:

# with flag only for < v16
node --experimental-repl-await

> const ksuid = await import("@thi.ng/ksuid");

Package sizes (gzipped, pre-treeshake): ESM: 827 bytes

Dependencies

API

Generated API docs

import { defKSUID32, defKSUID64, defULID } from "@thi.ng/ksuid";

// init 32bit epoch (resolution: seconds) w/ defaults
const id = defKSUID32();
// init 64bit epoch (resolution: milliseconds), same API
const id = defKSUID64();
// init 48bit epoch (resolution: milliseconds), same API
const id = defULID();

id.next();
// '05XCWbXa3akRqLDBUw4ogCVKGkd'

const a = id.nextBinary()
// Uint8Array(20) [
//     0, 160,  48, 77, 101, 251,
//   244,  17, 155, 97,  24, 101,
//    70,  71, 207, 23,  32,  21,
//   244, 116
// ]

// format a binary KSUID
id.format(a);
// '05XCZ32AaDZfZt0SWE2C22o6cqK'

id.parse("05XCZ32AaDZfZt0SWE2C22o6cqK")
// {
//   epoch: 1610498125000,
//   id: Uint8Array(16) [
//     101, 251, 244,  17, 155, 97,
//      24, 101,  70,  71, 207, 23,
//      32,  21, 244, 116
//   ]
// }

new Date(1610498125000).toISOString()
// '2021-01-13T00:35:25.000Z'

Creating custom IDs:

import { BASE36 } from "@thi.ng/base-n";

// no time shift, 64bit random
const id36 = defKSUID32({ base: BASE36, epoch: 0, bytes: 8 });
// '2VOUKH4K59AG0RXR4XH'

Benchmarks

yarn bench

benchmarking: b62, 128bit, n=10000
        warmup... 659.22ms (10 runs)
        executing...
        total: 6402.18ms, runs: 100
        mean: 64.02ms, median: 63.50ms, range: [59.98..96.15]
        q1: 62.64ms, q3: 64.41ms
        sd: 6.93%
benchmarking: b62, 64bit, n=10000
        warmup... 363.35ms (10 runs)
        executing...
        total: 3469.28ms, runs: 100
        mean: 34.69ms, median: 34.41ms, range: [32.61..56.58]
        q1: 33.35ms, q3: 35.41ms
        sd: 7.47%
benchmarking: b62, 32bit, n=10000
        warmup... 218.78ms (10 runs)
        executing...
        total: 2118.93ms, runs: 100
        mean: 21.19ms, median: 20.95ms, range: [20.20..25.74]
        q1: 20.71ms, q3: 21.30ms
        sd: 4.14%

Authors

Karsten Schmidt

If this project contributes to an academic publication, please cite it as:

@misc{thing-ksuid,
  title = "@thi.ng/ksuid",
  author = "Karsten Schmidt",
  note = "https://thi.ng/ksuid",
  year = 2020
}

License

© 2020 - 2021 Karsten Schmidt // Apache Software License 2.0

3.2.81

5 months ago

3.2.80

5 months ago

3.2.79

6 months ago

3.2.78

6 months ago

3.2.77

6 months ago

3.2.75

6 months ago

3.2.76

6 months ago

3.2.74

7 months ago

3.2.73

7 months ago

3.2.72

8 months ago

3.2.71

8 months ago

3.2.69

9 months ago

3.2.70

9 months ago

3.2.68

9 months ago

3.2.67

9 months ago

3.2.66

10 months ago

3.2.60

12 months ago

3.2.62

12 months ago

3.2.61

12 months ago

3.2.64

10 months ago

3.2.63

11 months ago

3.2.65

10 months ago

3.2.57

1 year ago

3.2.56

1 year ago

3.2.59

1 year ago

3.2.58

1 year ago

3.2.53

1 year ago

3.2.55

1 year ago

3.2.54

1 year ago

3.2.52

1 year ago

3.2.51

1 year ago

3.2.50

1 year ago

3.2.49

1 year ago

3.2.48

1 year ago

3.2.47

1 year ago

3.2.46

1 year ago

3.2.45

1 year ago

3.2.44

1 year ago

3.2.43

1 year ago

3.2.42

1 year ago

3.2.41

1 year ago

3.2.40

1 year ago

3.2.39

1 year ago

3.2.38

1 year ago

3.2.37

1 year ago

3.2.35

1 year ago

3.2.36

1 year ago

3.2.34

1 year ago

3.2.33

1 year ago

3.2.32

1 year ago

3.2.29

2 years ago

3.2.31

2 years ago

3.2.30

2 years ago

3.2.27

2 years ago

3.2.26

2 years ago

3.2.24

2 years ago

3.2.25

2 years ago

3.2.23

2 years ago

3.2.22

2 years ago

3.2.21

2 years ago

3.2.20

2 years ago

3.2.19

2 years ago

3.2.2

2 years ago

3.2.1

2 years ago

3.2.0

2 years ago

3.2.6

2 years ago

3.2.5

2 years ago

3.2.4

2 years ago

3.2.3

2 years ago

3.2.9

2 years ago

3.2.8

2 years ago

3.2.7

2 years ago

3.2.13

2 years ago

3.2.12

2 years ago

3.2.14

2 years ago

3.2.17

2 years ago

3.2.16

2 years ago

3.2.18

2 years ago

3.2.11

2 years ago

3.2.10

2 years ago

3.1.14

2 years ago

3.1.13

2 years ago

3.1.15

2 years ago

3.1.17

2 years ago

3.1.12

2 years ago

3.1.11

2 years ago

3.1.10

2 years ago

3.1.9

2 years ago

3.1.8

2 years ago

3.1.7

2 years ago

3.1.6

2 years ago

3.1.5

2 years ago

3.1.3

2 years ago

3.1.4

2 years ago

3.1.2

2 years ago

3.0.1

2 years ago

3.1.1

2 years ago

3.0.0

3 years ago

2.1.28

3 years ago

2.1.29

3 years ago

2.1.30

3 years ago

2.1.31

3 years ago

2.1.19

3 years ago

2.1.27

3 years ago

2.1.25

3 years ago

2.1.26

3 years ago

2.1.23

3 years ago

2.1.24

3 years ago

2.1.21

3 years ago

2.1.22

3 years ago

2.1.20

3 years ago

2.1.18

3 years ago

2.1.17

3 years ago

2.1.16

3 years ago

2.1.14

3 years ago

2.1.15

3 years ago

2.1.12

3 years ago

2.1.13

3 years ago

2.1.11

3 years ago

2.1.8

3 years ago

2.1.7

3 years ago

2.1.9

3 years ago

2.1.10

3 years ago

2.1.6

3 years ago

2.1.5

3 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.4

4 years ago

2.1.3

4 years ago

2.0.8

4 years ago

2.1.0

4 years ago

2.0.7

4 years ago

2.0.4

4 years ago

2.0.6

4 years ago

2.0.3

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.3

4 years ago

0.3.0

4 years ago

0.4.0

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.10

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

5 years ago