Skip to content

Commit

Permalink
Merge pull request #249 from screeny05/master
Browse files Browse the repository at this point in the history
Use DataView instead of Buffer in hand-written benchmark
  • Loading branch information
keichi authored Nov 22, 2023
2 parents b99ae40 + d24d56b commit 39912a8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 28 deletions.
23 changes: 12 additions & 11 deletions benchmark/bench.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const Benchmark = require("benchmark");
const bp = require("binparse").bp;
const Parser = require("../dist/binary_parser").Parser;
const Destruct = require("destruct-js");
const Struct = require("structron");
import Benchmark from "benchmark";
import { bp } from "binparse";
import { Parser } from "../dist/binary_parser.js";
import Destruct from "destruct-js";
import Struct from "structron";

const suite = new Benchmark.Suite();

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

const PointsParser = bp.object("SimpleObject", {
length: bp.variable("len", bp.lu32),
points: bp.array("Points", PointParser, "len"),
length: bp.lu32,
points: bp.array("Points", PointParser, "length"),
});

// binary-parser
Expand Down Expand Up @@ -64,13 +64,14 @@ for (let i = 0; i < n; i++) {
// Run benchmarks
suite
.add("hand-written", function () {
const n = buf.readUInt32LE(0);
const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
const n = view.getUint32(0, true);
const points = [];
for (let i = 0; i < n; i++) {
points.push({
x: buf.readUInt16LE(i * 6 + 0 + 4),
y: buf.readUInt16LE(i * 6 + 2 + 4),
z: buf.readUInt16LE(i * 6 + 4 + 4),
x: view.getUint16(i * 6 + 0 + 4, true),
y: view.getUint16(i * 6 + 2 + 4, true),
z: view.getUint16(i * 6 + 4 + 4, true),
});
}
})
Expand Down
31 changes: 17 additions & 14 deletions benchmark/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions benchmark/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"dependencies": {
"benchmark": "^2.1.4",
"binparse": "1.2.1",
"destruct-js": "^0.2.9",
"binparse": "^2.1.0",
"destruct-js": "^0.2.13",
"structron": "^0.4.3"
}
},
"type": "module"
}

0 comments on commit 39912a8

Please sign in to comment.