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
77const suite = new Benchmark . Suite ( ) ;
88
@@ -14,8 +14,8 @@ const PointParser = bp.object("Point", {
1414} ) ;
1515
1616const 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
6565suite
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 } )
0 commit comments