Skip to content

Commit

Permalink
Merge pull request #12 from theevenstarspace/next
Browse files Browse the repository at this point in the history
v1.0.0
  • Loading branch information
SergiiSharpov authored Dec 15, 2024
2 parents 71a8be3 + 10e5e48 commit 5e3c5cb
Show file tree
Hide file tree
Showing 56 changed files with 937 additions and 829 deletions.
26 changes: 16 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Byteform

![GitHub branch check runs](https://img.shields.io/github/check-runs/theevenstarspace/byteform/main?style=flat)
![npm package minimized gzipped size (scoped)](https://img.shields.io/bundlejs/size/%40evenstar/byteform?style=flat)
![NPM Version](https://img.shields.io/npm/v/%40evenstar%2Fbyteform?style=flat)
![GitHub branch check runs][checks]
[![Build Size][build-size]][build-size-url]
[![NPM Package][npm]][npm-url]

**Byteform** is a lightweight and versatile TypeScript library designed for encoding and decoding binary data. It provides an intuitive API to work with binary structures, making it an excellent choice for developers dealing with low-level data operations in both browser and Node.js environments.

Expand Down Expand Up @@ -70,12 +70,12 @@ const Player = new Struct({
### Encode data

```typescript
import { BinaryEncoder } from '@evenstar/byteform';
import { ByteStreamWriter } from '@evenstar/byteform';

const encoder = BinaryEncoder.create(1024); // 1KB buffer
const encoder = new ByteStreamWriter(1024); // 1KB buffer

// Encode a Player instance
encoder.encode(Player, {
encoder.writeSchema(Player, {
name: 'Alice',
level: 10,
position: { x: 1.0, y: 2.0, z: 3.0 },
Expand All @@ -97,19 +97,25 @@ encoder.encode(Player, {
* Get the encoded binary data
* Send the buffer over the network, save it to a file, etc.
*/
const buffer = encoder.commit();
const { buffer } = encoder.commit();
```

### Decode data

```typescript
import { BinaryDecoder } from '@evenstar/byteform';
import { ByteStreamReader } from '@evenstar/byteform';

const decoder = BinaryDecoder.fromArrayBuffer(buffer);
const player = decoder.decode(Player);
const decoder = new ByteStreamReader(buffer);
const player = decoder.readSchema(Player);

console.log(player);
```

## Contributing
Contributions are welcome! Please open an issue or submit a pull request to get involved.

[npm]: https://img.shields.io/npm/v/@evenstar/byteform?style=flat-square
[npm-url]: https://www.npmjs.com/package/@evenstar/byteform
[build-size]: https://flat.badgen.net/bundlephobia/minzip/@evenstar/byteform
[build-size-url]: https://bundlephobia.com/result?p=@evenstar/byteform
[checks]: https://img.shields.io/github/check-runs/theevenstarspace/byteform/main?style=flat-square
4 changes: 2 additions & 2 deletions benchmark/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@evenstar/byteform-benchmark",
"version": "0.9.2",
"version": "1.0.0",
"description": "Byteform benchmark space",
"main": "src/index.ts",
"scripts": {
Expand All @@ -18,7 +18,7 @@
"tsx": "^4.19.2"
},
"dependencies": {
"@evenstar/byteform": "^0.9.2",
"@evenstar/byteform": "link:../",
"benny": "^3.7.1",
"bson": "^6.10.0",
"flatbuffers": "^24.3.25",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
{
"name": "Player decoding",
"date": "2024-11-29T22:51:54.323Z",
"version": "0.9.2",
"date": "2024-12-15T17:15:05.805Z",
"version": "1.0.0",
"results": [
{
"name": "Byteform",
"ops": 1911408,
"margin": 0.27,
"percentSlower": 21.49
"ops": 1312033,
"margin": 2.51,
"percentSlower": 26.69
},
{
"name": "JSON",
"ops": 682279,
"margin": 0.2,
"percentSlower": 71.98
"ops": 515914,
"margin": 0.56,
"percentSlower": 71.17
},
{
"name": "Msgpack-lite",
"ops": 182240,
"margin": 0.33,
"percentSlower": 92.51
"ops": 136780,
"margin": 0.4,
"percentSlower": 92.36
},
{
"name": "Flatbuffers",
"ops": 2434659,
"margin": 0.22,
"ops": 1789599,
"margin": 1.1,
"percentSlower": 0
},
{
"name": "BSON",
"ops": 724156,
"margin": 0.25,
"percentSlower": 70.26
"ops": 525847,
"margin": 1.23,
"percentSlower": 70.62
}
],
"fastest": {
Expand Down
45 changes: 45 additions & 0 deletions benchmark/results/byteform/player-encoding.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "Player encoding",
"date": "2024-12-15T17:16:48.999Z",
"version": "1.0.0",
"results": [
{
"name": "Byteform",
"ops": 814927,
"margin": 0.64,
"percentSlower": 0
},
{
"name": "JSON",
"ops": 705122,
"margin": 0.53,
"percentSlower": 13.47
},
{
"name": "Msgpack-lite",
"ops": 281329,
"margin": 0.89,
"percentSlower": 65.48
},
{
"name": "Flatbuffers",
"ops": 705143,
"margin": 0.42,
"percentSlower": 13.47
},
{
"name": "BSON",
"ops": 151379,
"margin": 2.01,
"percentSlower": 81.42
}
],
"fastest": {
"name": "Byteform",
"index": 0
},
"slowest": {
"name": "BSON",
"index": 4
}
}
27 changes: 27 additions & 0 deletions benchmark/results/js/buffer-slice.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "Buffer Slice",
"date": "2024-12-15T17:15:16.599Z",
"version": "1.0.0",
"results": [
{
"name": "ArrayBuffer",
"ops": 1986177,
"margin": 2.37,
"percentSlower": 38.99
},
{
"name": "Uint8Array",
"ops": 3255680,
"margin": 2.79,
"percentSlower": 0
}
],
"fastest": {
"name": "Uint8Array",
"index": 1
},
"slowest": {
"name": "ArrayBuffer",
"index": 0
}
}
27 changes: 27 additions & 0 deletions benchmark/results/js/string-decoding.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "String Decoding",
"date": "2024-12-15T17:15:48.943Z",
"version": "1.0.0",
"results": [
{
"name": "TextDecoder",
"ops": 9980192,
"margin": 0.84,
"percentSlower": 0
},
{
"name": "Manual utf8",
"ops": 4057171,
"margin": 1.17,
"percentSlower": 59.35
}
],
"fastest": {
"name": "TextDecoder",
"index": 0
},
"slowest": {
"name": "Manual utf8",
"index": 1
}
}
39 changes: 39 additions & 0 deletions benchmark/results/js/string-encoding.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "String Encoding",
"date": "2024-12-15T17:15:38.233Z",
"version": "1.0.0",
"results": [
{
"name": "TextEncoder Direct",
"ops": 15270747,
"margin": 1.71,
"percentSlower": 0
},
{
"name": "TextEncoder Indirect (Set)",
"ops": 2691057,
"margin": 1.24,
"percentSlower": 82.38
},
{
"name": "TextEncoder Indirect (Manual)",
"ops": 2117128,
"margin": 1.29,
"percentSlower": 86.14
},
{
"name": "Manual utf8",
"ops": 11836180,
"margin": 0.91,
"percentSlower": 22.49
}
],
"fastest": {
"name": "TextEncoder Direct",
"index": 0
},
"slowest": {
"name": "TextEncoder Indirect (Manual)",
"index": 2
}
}
45 changes: 0 additions & 45 deletions benchmark/results/player-encoding.json

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions benchmark/src/byteform/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './player-encoding';
export * from './player-decoding';
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import benny from 'benny';
import msgpack from 'msgpack-lite';
import { BSON } from 'bson';
import { BinaryDecoder, BinaryEncoder, f32, List, Struct, text } from '@evenstar/byteform';
import { getOptions } from './utils';
import { ByteStreamReader, ByteStreamWriter, f32, List, Struct, text } from '@evenstar/byteform';
import { cleanup, getOptions } from '../utils';
import type { Summary } from 'benny/lib/internal/common-types';
import { encodePlayerFlatbuffer } from './flatbuffers/encode';
import { decodePlayerFlatbuffer } from './flatbuffers/decode';
Expand All @@ -24,9 +24,9 @@ export const PlayerDecoding = (): Promise<Summary> => benny.suite(
});

/** CREATE ENCODER AND ENCODE PLAYER **/
const encoder = BinaryEncoder.create(128);
const encoder = new ByteStreamWriter(128);

encoder.encode(player, {
encoder.writeSchema(player, {
name: 'Player',
position: { x: 1, y: 2, z: 3 },
rotation: 0,
Expand All @@ -42,10 +42,10 @@ export const PlayerDecoding = (): Promise<Summary> => benny.suite(
/** CYCLE FUNCTION **/
return (): void => {
// Create a decoder from the encoded data
const decoder = BinaryDecoder.fromArrayBuffer(buffer);
const decoder = new ByteStreamReader(buffer);

// Decode the player object
decoder.decode(player);
decoder.readSchema(player);
};
}),

Expand Down Expand Up @@ -115,7 +115,7 @@ export const PlayerDecoding = (): Promise<Summary> => benny.suite(
}),

benny.cycle(),
benny.complete(),
benny.complete(cleanup),

benny.save(getOptions('player-decoding')),
benny.save(getOptions('byteform', 'player-decoding')),
);
Loading

0 comments on commit 5e3c5cb

Please sign in to comment.