0.0.0-2a5b382 • Published 6 years ago

@bnaya/objectbuffer v0.0.0-2a5b382

Weekly downloads
20
License
MIT
Repository
github
Last release
6 years ago

ObjectBuffer: object-like API, backed by a sharedarraybuffer

npm version Coverage Status

The readme is for latest release v0.10.0.big refactor ongoing (allocator, hashmap)

For Modern browsers and node. Zero direct dependencies.

Save, read and update plain javascript objects into ArrayBuffer (And not only TypedArrays), using regular javascript object api, without serialization/deserialization.
Look at it as a simple implementation of javascript objects in user-land.

That's enables us to transfer or share objects in-memory with WebWorker without additional memory or serialization

The library is still not 1.0, but already usable, and will never offer full compatibility with plain js (Symbol and such)

For in-depth overview of how things are implemented, see implementation details document

Quick example

import { createObjectBuffer, getUnderlyingArrayBuffer } from "@bnaya/objectbuffer";

const initialValue = {
  foo: { bar: new Date(), arr: [1], nesting:{ WorksTM: true } }
};
// ArrayBuffer is created under the hood
const myObject = createObjectBuffer(
  {
    // available globally in the browser, or inside `util` in node
    textEncoder: new TextEncoder(),
    textDecoder: new TextDecoder()
  },
  // size in bytes
  1024,
  initialValue
);

const arrayBuffer = getUnderlyingArrayBuffer(myObject);

myObject.additionalProp = "new Value";
myObject.arr.push(2);

Play with it (codesandbox)

See also main.js for shared memory example. to run it: clone the repo, yarn install and yarn browser-playground

API reference

link

Why

Exchanging plain objects with WebWorkers is done by serializing and copying the data to the other side.
for some use-cases, it's slow and memory expensive.
ArrayBuffer can be transferred without a copy, and SharedArrayBuffer can be directly shared, but out of the box, it's hard to use ArrayBuffer as more than a TypedArray.

Why not FlatBuffers

For many cases FlatBuffers is the right tool!
FlatBuffers requires full re-serialization when changing values. inside. The api is also more different than javascript objects.

Disclaimer / Motivation

I'm working on it mostly from personal interest, and i'm not using it for any project yet.
Before putting any eggs in the basket, please go over the implementation details document

What's working

  • strings
  • number
  • objects (with nesting and all)
  • arrays
  • Date
  • Internal references (foo.bar2 = foo.bar will not create a copy)
  • Internal equality between objects (foo.bar === foo.bar will be true)
  • global lock for shared memory using Atomics (i hope its really working)

Caveats & Limitations

  • Need to specify size for the ArrayBuffer. When exceed that size, exception will be thrown. (Can be extended with a utility function, but not automatically)
  • Not memory re-use. memory allocation is append based, or overwrite when possible #21
  • Object are implemented using simple linked list #26
  • Maps & Sets are not supported yet #15 & #24
  • No prototype chain. objects props will simply be copied
  • Additional props on Array, Date, primitives will not be saved.
  • getters, setters, will not work/break the library

What's not working yet, but can be

  • bigint bigger than 64 bit

What's probably never going to work (convince me otherwise )

  • Anything that cannot go into JSON.stringify
  • Symbol

Contribution / Collaboration

There's a huge place for optimizations, code hygiene, and features!
Feel free to open issues and maybe implementing missing parts

0.32.1

2 years ago

0.32.0

2 years ago

0.31.2

4 years ago

0.31.1

4 years ago

0.30.1

5 years ago

0.30.0

5 years ago

0.0.0-e15856c

5 years ago

0.0.0-13b6f0c

5 years ago

0.0.0-85c3eef

5 years ago

0.0.0-8fdae1e

6 years ago

0.0.0-605c162

6 years ago

0.0.0-a2492fb

6 years ago

0.0.0-2efd336

6 years ago

0.0.0-8b87b26

6 years ago

0.0.0-af35656

6 years ago

0.0.0-a61adb4

6 years ago

0.0.0-13ca6cd

6 years ago

0.0.0-0f07a95

6 years ago

0.0.0-124044b

6 years ago

0.0.0-60ef1d1

6 years ago

0.0.0-742b4ef

6 years ago

0.0.0-7a3f3a1

6 years ago

0.0.0-9c76072

6 years ago

0.0.0-72a6034

6 years ago

0.0.0-39b3009

6 years ago

0.0.0-e79f5f5

6 years ago

0.0.0-9f82500

6 years ago

0.0.0-da30950

6 years ago

0.0.0-c89694e

6 years ago

0.0.0-428dc50

6 years ago

0.0.0-f4e1175

6 years ago

0.0.0-4c1a6d7

6 years ago

0.0.0-f658a9a

6 years ago

0.0.0-7449ff0

6 years ago

0.0.0-a954c8a

6 years ago

0.0.0-ea4bed6

6 years ago

0.0.0-178b1dc

6 years ago

0.0.0-b77374c

6 years ago

0.0.0-ab8ba96

6 years ago

0.0.0-b8786dd

6 years ago

0.0.0-a914a65

6 years ago

0.0.0-51c2fb2

6 years ago

0.0.0-a02532b

6 years ago

0.0.0-1ed1bee

6 years ago

0.0.0-f463ae1

6 years ago

0.0.0-e1b2324

6 years ago

0.0.0-4dc8259

6 years ago

0.0.0-50e77f5

6 years ago

0.0.0-1dd6a34

6 years ago

0.0.0-371880c

6 years ago

0.0.0-09670aa

6 years ago

0.0.0-4a8438f

6 years ago

0.0.0-11da881

6 years ago

0.0.0-3d4c1c8

6 years ago

0.0.0-9dd1a9f

6 years ago

0.0.0-7c93e52

6 years ago

0.0.0-8acf465

6 years ago

0.0.0-bab722f

6 years ago

0.0.0-b62e425

6 years ago

0.0.0-62e8d85

6 years ago

0.20.0

6 years ago

0.0.0-a2ec4b4

6 years ago

0.0.0-5731d54

6 years ago

0.0.0-68033db

6 years ago

0.0.0-f1ba10a

6 years ago

0.0.0-49fce16

6 years ago

0.0.0-6fcc0e0

6 years ago

0.0.0-c94c3aa

6 years ago

0.0.0-fc62f82

6 years ago

0.0.0-a3f2701

6 years ago

0.0.0-0ff2937

6 years ago

0.0.0-547d018

6 years ago

0.0.0-b8dcd6a

6 years ago

0.0.0-7d79fe6

6 years ago

0.0.0-273ae5d

6 years ago

0.0.0-a10c867

6 years ago

0.0.0-411b85b

6 years ago

0.0.0-02d52d5

6 years ago

0.0.0-96df3bb

6 years ago

0.0.0-31900c8

6 years ago

0.0.0-396bfa3

6 years ago

0.0.0-2a35d64

6 years ago

0.0.0-3ed6e3d

6 years ago

0.0.0-35bbc1f

6 years ago

0.0.0-8cc1a40

6 years ago

0.0.0-cd2fa24

6 years ago

0.0.0-00c51c3

6 years ago

0.0.0-dfae000

6 years ago

0.0.0-2a5b382

6 years ago

0.0.0-6f61de1

6 years ago

0.0.0-d75f30c

6 years ago

0.0.0-453549e

6 years ago

0.0.0-136a848

6 years ago

0.0.0-738fce6

6 years ago

0.0.0-acadd38

6 years ago

0.0.0-cfaa8f7

6 years ago

0.0.0-2baea38

6 years ago

0.0.0-6858e67

6 years ago

0.10.1

6 years ago

0.0.0-4166577

6 years ago

0.0.0-7eda641

6 years ago

0.0.0-4b90e23

6 years ago

0.0.0-0daa16b

6 years ago

0.0.0-c7c5f34

6 years ago

0.0.0-b14ab74

6 years ago

0.0.0-32b9c70

6 years ago

0.10.0

6 years ago

0.9.2

6 years ago

0.9.1

6 years ago

0.9.0

6 years ago

0.8.0

6 years ago

0.7.0

6 years ago

0.6.0

6 years ago

0.5.1

6 years ago

0.5.0

6 years ago

0.4.0

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago