Skip to content

Commit 39912a8

Browse files
authored
Merge pull request #249 from screeny05/master
Use DataView instead of Buffer in hand-written benchmark
2 parents b99ae40 + d24d56b commit 39912a8

File tree

3 files changed

+33
-28
lines changed

3 files changed

+33
-28
lines changed

benchmark/bench.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const Benchmark = require("benchmark");
2-
const bp = require("binparse").bp;
3-
const Parser = require("../dist/binary_parser").Parser;
4-
const Destruct = require("destruct-js");
5-
const Struct = require("structron");
1+
import Benchmark from "benchmark";
2+
import { bp } from "binparse";
3+
import { Parser } from "../dist/binary_parser.js";
4+
import Destruct from "destruct-js";
5+
import Struct from "structron";
66

77
const suite = new Benchmark.Suite();
88

@@ -14,8 +14,8 @@ const PointParser = bp.object("Point", {
1414
});
1515

1616
const PointsParser = bp.object("SimpleObject", {
17-
length: bp.variable("len", bp.lu32),
18-
points: bp.array("Points", PointParser, "len"),
17+
length: bp.lu32,
18+
points: bp.array("Points", PointParser, "length"),
1919
});
2020

2121
// binary-parser
@@ -64,13 +64,14 @@ for (let i = 0; i < n; i++) {
6464
// Run benchmarks
6565
suite
6666
.add("hand-written", function () {
67-
const n = buf.readUInt32LE(0);
67+
const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
68+
const n = view.getUint32(0, true);
6869
const points = [];
6970
for (let i = 0; i < n; i++) {
7071
points.push({
71-
x: buf.readUInt16LE(i * 6 + 0 + 4),
72-
y: buf.readUInt16LE(i * 6 + 2 + 4),
73-
z: buf.readUInt16LE(i * 6 + 4 + 4),
72+
x: view.getUint16(i * 6 + 0 + 4, true),
73+
y: view.getUint16(i * 6 + 2 + 4, true),
74+
z: view.getUint16(i * 6 + 4 + 4, true),
7475
});
7576
}
7677
})

benchmark/package-lock.json

Lines changed: 17 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

benchmark/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"dependencies": {
33
"benchmark": "^2.1.4",
4-
"binparse": "1.2.1",
5-
"destruct-js": "^0.2.9",
4+
"binparse": "^2.1.0",
5+
"destruct-js": "^0.2.13",
66
"structron": "^0.4.3"
7-
}
7+
},
8+
"type": "module"
89
}

0 commit comments

Comments
 (0)