0.2.93 • Published 1 month ago

lib0 v0.2.93

Weekly downloads
12,267
License
MIT
Repository
github
Last release
1 month ago

Lib0 Build Status

Monorepo of isomorphic utility functions

This library is meant to replace all global JavaScript functions with isomorphic module imports. Additionally, it implements several performance-oriented utility modules. Most noteworthy are the binary encoding/decoding modules lib0/encoding / lib0/decoding, the randomized testing framework lib0/testing, the fast Pseudo Random Number Generator lib0/PRNG, the small socket.io alternative lib0/websocket, and the logging module lib0/logging that allows colorized logging in all environments. Lib0 has only one dependency, which is also from the author of lib0. If lib0 is transpiled with rollup or webpack, very little code is produced because of the way that it is written. All exports are pure and are removed by transpilers that support dead code elimination. Here is an example of how dead code elemination and mangling optimizes code from lib0:

// How the code is optimized by transpilers:

// lib0/json.js
export const stringify = JSON.stringify
export const parse = JSON.parse

// index.js
import * as json from 'lib0/json'
export const f = (arg1, arg2) => json.stringify(arg1) + json.stringify(arg2)

// compiled with rollup and uglifyjs:
const s=JSON.stringify,f=(a,b)=>s(a)+s(b)
export {f}

Performance resources

Each function in this library is tested thoroughly and is not deoptimized by v8 (except some logging and comparison functions that can't be implemented without deoptimizations). This library implements its own test suite that is designed for randomized testing and inspecting performance issues.

Code style

The code style might be a bit different from what you are used to. Stay open. Most of the design choices have been thought through. The purpose of this code style is to create code that is optimized by the compiler and that results in small code bundles when used with common module bundlers. Keep that in mind when reading the library.

  • No polymorphism!
  • Modules should only export pure functions and constants. This way the module bundler can eliminate dead code. The statement const x = someCondition ? A : B cannot be eleminated, because it is tied to a condition.
  • Use Classes for structuring data. Classes are well supported by jsdoc and are immediately optimized by the compiler. I.e. prefer class Coord { constructor (x, y) { this.x = x; this.y = y} } instead of { x: x, y: y }, because the compiler needs to be assured that the order of properties does not change. { y: y, x: x } has a different hidden class than { x: x, y: y }, which will lead to code deoptimizations if their use is alternated.
  • The user of your module should never create data objects with the new keyword. Prefer exporting factory functions like const createCoordinate = (x, y) => new Coord(x, y).
  • The use of class methods is discouraged, because method names can't be mangled or removed by dead code elimination.
  • The only acceptable use of methods is when two objects implement functionality differently. E.g. class Duck { eat () { swallow() } } and class Cow { eat () { chew() } } have the same signature, but implement it differently.
  • Prefer const variable declarations. Use let only in loops. const always leads to easier code.
  • Keep the potential execution stack small and compartmentalized. Nobody wants to debug spaghetti code.
  • Give proper names to your functions and ask yourself if you would know what the function does if you saw it in the execution stack.
  • Avoid recursion. There is a stack limit in most browsers and not every recursive function is optimized by the compiler.
  • Semicolons are superfluous. Lint with https://standardjs.com/

Using lib0

lib0 contains isomorphic modules that work in nodejs, the browser, and other environments. It exports modules as the commonjs and the new esm module format.

If possible,

ESM module

import module from 'lib0/[module]' // automatically resolves to lib0/[module].js

CommonJS

require('lib0/[module]') // automatically resolves to lib0/dist/[module].cjs

Manual

Automatically resolving to commonjs and esm modules is implemented using conditional exports which is available in node>=v12. If support for older versions is required, then it is recommended to define the location of the module manually:

import module from 'lib0/[module].js'
// require('lib0/dist/[module].cjs')

Modules

@dxos/document@dxos/editornephele-y-websocket@cosmicverse/foundationy-solid@everything-registry/sub-chunk-2066y-azure-webpubsub-devy-codemirrory-codemirror.jhy-codemirror.nexty-codemirror.next.davidrd123y-colour-websockety-daty-azure-webpubsuby-azure-webpubsub-clienty-indexeddby-generic-syncy-durableobjectsy-dynamodby-dynamodb-for-sdkv3y-partykity-pingerchipsy-prosemirrory-prosemirror-lat-specialy-protocolsy-protocols-node14y-mongodb-optimizedy-mongodb-providery-leveldby-localforagey-localstoragey-lwwmapy-quilly-redisy-monacoy-monaco-hruthiky-mongodby-workery-webcomponentsy-webrtcy-websockety-websocket-authy-websocket-multiplexingy-taubytey-utilityy-socket.ioy-webtransportyicat-y-websocketyrb-actioncable@editablejs/plugin-protocols@editablejs/plugin-yjs-websocket@editablejs/protocols@editablejs/yjs-protocols@editablejs/yjs-websocket@editablejs/plugin-codeblockyjsyjs-node14yjs-reduxyjs-wyjs-widgetsyjs-clj@dxos/aurora-composer@dxos/react-ui-composer@dxos/react-ui-editor@dxos/react-composertldrawlignintype-created-yjs@everynote/protocols@everynote/yjs-protocols@everynote/yjs-websocket@everynote/plugin-codeblock@foxitsoftware/web-collab-client@foxitsoftware/web-collab-serverv21.7.0@inquisitive/serverless-y-websocket@iojcde/y-partykit@isener/y-quill@hocuspocus/common@hocuspocus/provider@hocuspocus/server@hanfy/plugin-yjs-protocols@hanfy/plugin-yjs-websocket@hedgedoc/realtime@hedgedoc/realtime-communication@grotelee/provider@grotelee/y-websocketcomfy-creatorreflect-yjssedestral-y-quillsedestral-y-websocketsb-yy-websocketsituated@opensumi/ide-collaboration@keystatic/core@lamkoti/y-mongodb-provider@miii/y-google-cast@getoutline/y-prosemirror@gagndeep/y-webrtc@gived/waving@gived/yjs
0.2.93

1 month ago

0.2.92

1 month ago

0.2.91

2 months ago

0.2.90

2 months ago

0.2.89

2 months ago

0.2.88

5 months ago

0.2.85

8 months ago

0.2.84

8 months ago

0.2.87

7 months ago

0.2.86

7 months ago

0.2.83

8 months ago

0.2.82

9 months ago

0.2.81

9 months ago

0.2.80

9 months ago

0.2.79

9 months ago

0.2.78

11 months ago

0.2.77

11 months ago

0.2.76

11 months ago

0.2.75

11 months ago

0.2.74

1 year ago

0.2.63

1 year ago

0.2.62

1 year ago

0.2.61

1 year ago

0.2.60

1 year ago

0.2.69

1 year ago

0.2.68

1 year ago

0.2.67

1 year ago

0.2.66

1 year ago

0.2.65

1 year ago

0.2.64

1 year ago

0.2.59

1 year ago

0.2.73

1 year ago

0.2.72

1 year ago

0.2.71

1 year ago

0.2.70

1 year ago

0.2.58

1 year ago

0.2.57

1 year ago

0.2.56

1 year ago

0.2.55

1 year ago

0.2.54

1 year ago

0.2.53

1 year ago

0.2.52

2 years ago

0.2.51

2 years ago

0.2.50

2 years ago

0.2.49

2 years ago

0.2.48

2 years ago

0.2.47

2 years ago

0.2.46

2 years ago

0.2.45

2 years ago

0.2.44

2 years ago

0.2.43

2 years ago

0.2.42

3 years ago

0.2.41

3 years ago

0.2.40

3 years ago

0.2.39

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

4 years ago

0.2.33

4 years ago

0.2.32

4 years ago

0.2.31

4 years ago

0.2.30

4 years ago

0.2.29

4 years ago

0.2.28

4 years ago

0.2.27

4 years ago

0.2.26

4 years ago

0.2.25

4 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.4

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.2

4 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago

0.0.0

5 years ago