Skip to content

Commit 739638b

Browse files
0.4.0
Added int_53 data representation. Replaced DataView with Uint8Array.
1 parent b81b995 commit 739638b

14 files changed

+340
-250
lines changed

README.md

+19-22
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
![CI](https://github.com/username0101010/array-buffer-serializer/actions/workflows/test.yml/badge.svg)
44
[![codecov](https://codecov.io/gh/username0101010/array-buffer-serializer/branch/main/graph/badge.svg?token=IZFQQP34H7)](https://codecov.io/gh/username0101010/array-buffer-serializer)
55

6-
Allows to encode an object into bytes before transmission via WebRTC or WebSocket and decode it back when received.
7-
This is very useful for network bandwidth when you need to send data to one or more recipients frequently.
6+
Similar (without fixed length data representation and some other differences) as [CBOR](https://datatracker.ietf.org/doc/html/rfc7049) or [MessagePack](https://github.com/msgpack/msgpack/blob/master/spec.md), allows to encode an object into bytes before transmission via WebRTC or WebSocket and decode it back when received.
7+
This is very useful for network bandwidth when you need to send data to one or more recipients frequently. Works in both: browser and node.
88

99
Some characters of the [Latin-1 Supplement](https://en.wikipedia.org/wiki/Latin-1_Supplement_(Unicode_block))
10-
are reserved for encoding values: C0 (U+00C0) - E3 (U+00E7).
10+
are reserved for encoding values: C0 (U+00C0) - E3 (U+00E7). Supports only ASCII string.
1111

1212
## Installation
1313

@@ -37,9 +37,9 @@ yarn add array-buffer-serializer
3737
2. **Encode some data (object or array) using "toBuffer" method**
3838

3939
```javascript
40-
// as object
41-
const data = { this: "way" };
42-
// as array
40+
// as map
41+
const data = {this: "way"};
42+
// or array
4343
const data = ["or", "that", "way"];
4444
4545
const buffer = Serializer.toBuffer(data);
@@ -58,19 +58,18 @@ yarn add array-buffer-serializer
5858
});
5959
```
6060

61-
## Table
62-
61+
## Compression
6362
<table>
6463
<tr>
6564
<th rowspan="2">Type</th>
6665
<th colspan="2">Data</th>
6766
<th colspan="2">Bytes</th>
6867
</tr>
6968
<tr>
70-
<th>Object</th>
71-
<th>Raw</th>
72-
<th>Object</th>
73-
<th>Raw</th>
69+
<th>Object (decoded)</th>
70+
<th>Raw (encoded)</th>
71+
<th>Decoded</th>
72+
<th>Encoded</th>
7473
</tr>
7574
<tr>
7675
<td>undefined</td>
@@ -80,14 +79,13 @@ yarn add array-buffer-serializer
8079
<td>2</td>
8180
</tr>
8281
<tr>
83-
<td>true</td>
82+
<td rowspan="2">boolean</td>
8483
<td>[true]</td>
8584
<td><00 d2></td>
8685
<td>6</td>
8786
<td>2</td>
8887
</tr>
8988
<tr>
90-
<td>false</td>
9189
<td>[false]</td>
9290
<td><00 d0></td>
9391
<td>7</td>
@@ -128,6 +126,13 @@ yarn add array-buffer-serializer
128126
<td>12</td>
129127
<td>6</td>
130128
</tr>
129+
<tr>
130+
<td>int53_t</td>
131+
<td>[9007199254740991]</td>
132+
<td><00 cc ff ff ff ff ff ff 1f></td>
133+
<td>18</td>
134+
<td>9</td>
135+
</tr>
131136
<tr>
132137
<td>bigint</td>
133138
<td>[-18446744073709551615n]</td>
@@ -151,14 +156,6 @@ yarn add array-buffer-serializer
151156
</tr>
152157
</table>
153158

154-
## Features
155-
156-
* No model is needed to describe the data structure
157-
* Uses type definition instead of object's key-value separator ":" and array items delimiter ","
158-
* Uses unsigned data representation by default (uint8_t, uint16_t...)
159-
* Different marks for positive and negative numbers, so negative sign is for free
160-
* Marks are divided into even and odd, each odd mark indicates at the first array value
161-
162159
## License
163160

164161
[MIT](./LICENSE)

benchmarks/decoder-suite.js

+38-31
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ const marks = require("./../lib/marks.js");
55
module.exports = function() {
66
const suite = new Benchmark.Suite;
77

8-
let decoder, dv, ab;
8+
let decoder, view;
99

1010
function resetBuffer() {
11-
ab = new ArrayBuffer(64);
12-
dv = new DataView(ab);
13-
dv.setUint8(0, 0);
11+
view = new Uint8Array(64);
12+
view[0] = 0;
1413
}
1514

1615
const opts = {
@@ -22,82 +21,90 @@ module.exports = function() {
2221
.add(
2322
"Decoder#Null",
2423
function() {
25-
dv.setUint8(1, marks.DEFAULT_MARK_NULL);
26-
decoder = new BufferDecoder(dv);
24+
view[1] = marks.DEFAULT_MARK_NULL;
25+
decoder = new BufferDecoder(view.buffer);
2726
decoder.decode();
2827
},
2928
opts
3029
)
3130
.add(
3231
"Decoder#Array",
3332
function() {
34-
dv.setUint8(1, marks.DEFAULT_MARK_INT8 + 1);
35-
dv.setUint8(2, 15);
36-
decoder = new BufferDecoder(dv);
33+
view[1] = marks.DEFAULT_MARK_INT8 + 1;
34+
view[2] = 15;
35+
decoder = new BufferDecoder(view.buffer);
3736
decoder.decode();
3837
},
3938
opts
4039
)
4140
.add(
4241
"Decoder#Dict",
4342
function() {
44-
dv.setUint8(1, marks.DEFAULT_MARK_OBJ_OPEN);
45-
dv.setUint8(2, 1);
46-
dv.setUint8(3, marks.DEFAULT_MARK_UINT8);
47-
dv.setUint8(4, 25);
48-
dv.setUint8(5, marks.DEFAULT_MARK_OBJ_CLOSE)
49-
decoder = new BufferDecoder(dv);
43+
view[1] = marks.DEFAULT_MARK_OBJ_OPEN;
44+
view[2] = 1;
45+
view[3] = marks.DEFAULT_MARK_UINT8;
46+
view[4] = 25;
47+
view[5] = marks.DEFAULT_MARK_OBJ_CLOSE;
48+
decoder = new BufferDecoder(view.buffer);
5049
for (let i = 0; i < 5; i++) decoder.decode();
5150
},
5251
opts
5352
)
5453
.add(
5554
"Decoder#String",
5655
function() {
57-
dv.setUint8(1, marks.DEFAULT_MARK_STR8);
58-
dv.setUint8(2, 1);
59-
dv.setUint8(3, 2);
60-
dv.setUint8(4, 3);
61-
dv.setUint8(5, marks.DEFAULT_MARK_STR8);
62-
decoder = new BufferDecoder(dv);
56+
view[1] = marks.DEFAULT_MARK_STR8;
57+
view[2] = 1;
58+
view[3] = 2;
59+
view[4] = 3;
60+
view[5] = marks.DEFAULT_MARK_STR8;
61+
decoder = new BufferDecoder(view.buffer);
6362
decoder.decode();
6463
},
6564
opts
6665
)
6766
.add(
6867
"Decoder#Boolean",
6968
function() {
70-
dv.setUint8(1, marks.DEFAULT_MARK_TBOOL);
71-
decoder = new BufferDecoder(dv);
69+
view[1] = marks.DEFAULT_MARK_TBOOL;
70+
decoder = new BufferDecoder(view.buffer);
7271
decoder.decode();
7372
},
7473
opts
7574
)
7675
.add(
7776
"Decoder#Undefined",
7877
function() {
79-
dv.setUint8(1, marks.DEFAULT_MARK_UNDEF);
80-
decoder = new BufferDecoder(dv);
78+
view[1] = marks.DEFAULT_MARK_UNDEF;
79+
decoder = new BufferDecoder(view.buffer);
8180
decoder.decode();
8281
},
8382
opts
8483
)
8584
.add(
8685
"Decoder#Bigint",
8786
function() {
88-
dv.setUint8(1, marks.DEFAULT_MARK_UBIGINT);
89-
dv.setBigUint64(2, 25n);
90-
decoder = new BufferDecoder(dv);
87+
view[1] = marks.DEFAULT_MARK_UBIGINT;
88+
view[2] = 0;
89+
view[3] = 0;
90+
view[4] = 0;
91+
view[5] = 0;
92+
view[6] = 0;
93+
view[7] = 0;
94+
view[8] = 0;
95+
view[9] = 25;
96+
decoder = new BufferDecoder(view.buffer);
9197
decoder.decode();
9298
},
9399
opts
94100
)
95101
.add(
96102
"Decoder#Number",
97103
function() {
98-
dv.setUint8(1, marks.DEFAULT_MARK_UINT16);
99-
dv.setUint16(2, 305);
100-
decoder = new BufferDecoder(dv);
104+
view[1] = marks.DEFAULT_MARK_UINT16;
105+
view[2] = 15;
106+
view[3] = 255;
107+
decoder = new BufferDecoder(view.buffer);
101108
decoder.decode();
102109
},
103110
opts

benchmarks/monolith-suite.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
const Benchmark = require("benchmark");
22
const Serializer = require("../lib/Serializer.js");
3-
const dictionary = require("../example/dictionary.js");
3+
const map = require("../example/map.js");
44
const array = require("../example/array.js");
55

66
BigInt.prototype.toJSON = function() { return this.toString(); }
77

88
const encoded = {
9-
dict: {
10-
JSON: JSON.stringify(dictionary),
11-
raw: Serializer.toBuffer(dictionary)
9+
map: {
10+
JSON: JSON.stringify(map),
11+
raw: Serializer.toBuffer(map)
1212
},
1313
array: {
1414
JSON: JSON.stringify(array),
@@ -21,9 +21,9 @@ module.exports = function() {
2121

2222
suite
2323
.add(
24-
"Decoder.dict#JSON",
24+
"Decoder.map#JSON",
2525
function() {
26-
JSON.parse(encoded.dict.JSON);
26+
JSON.parse(encoded.map.JSON);
2727
}
2828
)
2929
.add(
@@ -33,9 +33,9 @@ module.exports = function() {
3333
}
3434
)
3535
.add(
36-
"Decoder.dict#Serializer",
36+
"Decoder.map#Serializer",
3737
function() {
38-
Serializer.fromBuffer(encoded.dict.raw);
38+
Serializer.fromBuffer(encoded.map.raw);
3939
}
4040
)
4141
.add(
@@ -45,9 +45,9 @@ module.exports = function() {
4545
}
4646
)
4747
.add(
48-
"Encoder.dict#JSON",
48+
"Encoder.map#JSON",
4949
function() {
50-
JSON.stringify(dictionary);
50+
JSON.stringify(map);
5151
}
5252
)
5353
.add(
@@ -57,9 +57,9 @@ module.exports = function() {
5757
}
5858
)
5959
.add(
60-
"Encoder.dict#Serializer",
60+
"Encoder.map#Serializer",
6161
function() {
62-
Serializer.toBuffer(dictionary);
62+
Serializer.toBuffer(map);
6363
}
6464
)
6565
.add(

example/array.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ if (module.parent === null) {
2727
"Encoded length:",
2828
buffer.byteLength
2929
); // 54
30-
30+
3131
/// expect(data).toEqual(decoded); <--- true
3232
}
File renamed without changes.

0 commit comments

Comments
 (0)