2.2.41 • Published 2 days ago

@thi.ng/idgen v2.2.41

Weekly downloads
55
License
Apache-2.0
Repository
github
Last release
2 days ago

idgen

npm version npm downloads Twitter Follow

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

About

Generator of opaque numeric identifiers with optional support for ID versioning and efficient re-use.

Previously generated IDs that have been discarded are stored in a memory-efficient implicit list of free IDs and will be re-used. The overall range of IDs can be specified/limited at construction time and is based on a given bit width. The largest range currently supported is 32 bits, less if versioning is enabled (configurable).

If versioning is used, the produced IDs are composite values, i.e. the lowest bits contain the actual ID (e.g for indexing purposes) and other bits contain the version information.

composite ID

Both parts can be extracted via the generator's .id() and .version() methods. Each time a valid versioned ID is being discarded via .free(id), its version is being increased and, depending on use case and usage frequency, will eventually overflow back to 0. Once an ID's version has been updated, the old version is considered invalid. IDs can be checked for validity via .has(id) (in constant time).

Status

STABLE - used in production

Search or submit any issues for this package

Related packages

  • @thi.ng/ecs - Entity Component System based around typed arrays & sparse sets
  • @thi.ng/ksuid - Configurable K-sortable unique IDs, ULIDs, binary & base-N encoded, 32/48/64bit time resolutions

Installation

yarn add @thi.ng/idgen

ES module import:

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

Skypack documentation

For Node.js REPL:

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

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

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

Dependencies

API

Generated API docs

import { idgen } from "@thi.ng/idgen";

ID generator with 16 bit range and no versioning

const ids = idgen(16, 0);

ids.next();
// 0

ids.next();
// 1

ids.next(2);
// 2

// discard ID 0
ids.free(0);
// true

ids.has(0);
// false

// reuse
ids.next()
// 0

ids.has(0);
// true

ids.next()
// 3

ID generator w/ 24 bit range & 8 bit version range

// the 8bit version range is being deduced automatically (32-24 = 8),
// but can also be overwritten
const ids = idgen(24);

const a = ids.next();
// 0

ids.free(a);
// true

const b = ids.next();
// 16777216

// b is the re-used new version of a
ids.id(b);
// 0

ids.version(b)
// 1

ids.has(b);
// true

// a is invalid at this point
// (even though a's .id() part is the same as b's)
ids.has(a);
// false

IDGen is iterable

const ids = ig.idgen(8);

ids.next();
// 0
ids.next();
// 1
ids.next();
// 2
ids.next();
// 3

ids.free(2);
// true

// only currently used IDs are returned
// NO ordering guarantee!
[...ids]
// [ 3, 1, 0 ]

ids.next();
// 258

[...ids]
// [3, 258, 1, 0]

Authors

Karsten Schmidt

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

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

License

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

2.2.41

2 days ago

2.2.40

17 days ago

2.2.39

20 days ago

2.2.38

29 days ago

2.2.37

1 month ago

2.2.36

1 month ago

2.2.35

2 months ago

2.2.34

2 months ago

2.2.33

2 months ago

2.2.32

2 months ago

2.2.31

2 months ago

2.2.30

2 months ago

2.2.28

3 months ago

2.2.29

3 months ago

2.2.27

3 months ago

2.2.26

3 months ago

2.2.24

3 months ago

2.2.25

3 months ago

2.2.22

3 months ago

2.2.23

3 months ago

2.2.21

3 months ago

2.2.20

4 months ago

2.2.18

4 months ago

2.2.19

4 months ago

2.2.17

5 months ago

2.2.16

5 months ago

2.2.15

5 months ago

2.2.14

5 months ago

2.2.1

9 months ago

2.2.0

9 months ago

2.2.3

9 months ago

2.2.2

9 months ago

2.2.13

5 months ago

2.2.5

9 months ago

2.2.4

9 months ago

2.2.11

6 months ago

2.2.7

7 months ago

2.2.12

6 months ago

2.2.6

9 months ago

2.2.10

6 months ago

2.2.9

6 months ago

2.2.8

7 months ago

2.1.34

11 months ago

2.1.33

1 year ago

2.1.32

1 year ago

2.1.29

1 year ago

2.1.30

1 year ago

2.1.31

1 year ago

2.1.28

1 year ago

2.1.27

1 year ago

2.1.25

1 year ago

2.1.26

1 year ago

2.1.23

1 year ago

2.1.24

1 year ago

2.1.21

1 year ago

2.1.22

1 year ago

2.1.16

2 years ago

2.1.17

2 years ago

2.1.14

2 years ago

2.1.15

2 years ago

2.1.18

1 year ago

2.1.19

1 year ago

2.1.20

1 year ago

2.1.13

2 years ago

2.1.12

2 years ago

2.1.9

2 years ago

2.1.10

2 years ago

2.1.11

2 years ago

2.1.8

2 years ago

2.1.6

2 years ago

2.1.7

2 years ago

2.1.5

2 years ago

2.1.4

2 years ago

2.0.8

2 years ago

2.1.2

2 years ago

2.1.1

2 years ago

2.1.3

2 years ago

2.1.0

2 years ago

2.0.7

3 years ago

2.0.4

3 years ago

2.0.6

3 years ago

2.0.3

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.3

3 years ago

0.2.38

3 years ago

0.2.37

3 years ago

0.2.36

3 years ago

0.2.35

3 years ago

0.2.34

3 years ago

0.2.33

3 years ago

0.2.32

3 years ago

0.2.31

3 years ago

0.2.30

3 years ago

0.2.29

3 years ago

0.2.28

3 years ago

0.2.27

3 years ago

0.2.26

3 years ago

0.2.25

3 years ago

0.2.24

4 years ago

0.2.23

4 years ago

0.2.22

4 years ago

0.2.21

4 years ago

0.2.20

4 years ago

0.2.19

4 years ago

0.2.18

4 years ago

0.2.17

4 years ago

0.2.16

4 years ago

0.2.15

4 years ago

0.2.14

4 years ago

0.2.13

4 years ago

0.2.12

4 years ago

0.2.11

4 years ago

0.2.10

4 years ago

0.2.9

4 years ago

0.2.8

4 years ago

0.2.7

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.4

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.0

4 years ago