1- var binary = require ( ' binary' ) ;
2- var Benchmark = require ( ' benchmark' ) ;
3- var bp = require ( ' binparse' ) . bp ;
4- var Parser = require ( 'binary-parser' ) . Parser ;
5- var Destruct = require ( ' destruct-js' ) ;
6- const Struct = require ( ' structron' ) ;
1+ var binary = require ( " binary" ) ;
2+ var Benchmark = require ( " benchmark" ) ;
3+ var bp = require ( " binparse" ) . bp ;
4+ var Parser = require ( "../dist/binary_parser" ) . Parser ;
5+ var Destruct = require ( " destruct-js" ) ;
6+ const Struct = require ( " structron" ) ;
77
8- var suite = new Benchmark . Suite ;
8+ var suite = new Benchmark . Suite ( ) ;
99
1010// binparse
11- const PointParser = bp . object ( ' Point' , {
12- x : bp . lu16 ,
13- y : bp . lu16 ,
14- z : bp . lu16 ,
11+ const PointParser = bp . object ( " Point" , {
12+ x : bp . lu16 ,
13+ y : bp . lu16 ,
14+ z : bp . lu16 ,
1515} ) ;
1616
17- const PointsParser = bp . object ( ' SimpleObject' , {
18- length : bp . variable ( ' len' , bp . lu32 ) ,
19- points : bp . array ( ' Points' , PointParser , ' len' ) ,
17+ const PointsParser = bp . object ( " SimpleObject" , {
18+ length : bp . variable ( " len" , bp . lu32 ) ,
19+ points : bp . array ( " Points" , PointParser , " len" ) ,
2020} ) ;
2121
2222// binary-parser
23- const Points = new Parser ( )
24- . uint32le ( 'len' )
25- . array ( 'points' , {
26- length : 'len' ,
27- type : new Parser ( )
28- . uint16le ( 'x' )
29- . uint16le ( 'y' )
30- . uint16le ( 'z' )
31- } ) ;
23+ const Points = new Parser ( ) . uint32le ( "len" ) . array ( "points" , {
24+ length : "len" ,
25+ type : new Parser ( ) . uint16le ( "x" ) . uint16le ( "y" ) . uint16le ( "z" ) ,
26+ } ) ;
3227
3328// destruct-js
34- const spec = new Destruct . Spec ( { mode : Destruct . Mode . LE } ) ;
35- spec . field ( 'len' , Destruct . UInt32 )
36- . loop ( 'points' , ( r ) => r . len , new Destruct . Spec ( { mode : Destruct . Mode . LE } )
37- . field ( 'x' , Destruct . UInt16 )
38- . field ( 'y' , Destruct . UInt16 )
39- . field ( 'z' , Destruct . UInt16 ) ) ;
29+ const spec = new Destruct . Spec ( { mode : Destruct . Mode . LE } ) ;
30+ spec
31+ . field ( "len" , Destruct . UInt32 )
32+ . loop (
33+ "points" ,
34+ ( r ) => r . len ,
35+ new Destruct . Spec ( { mode : Destruct . Mode . LE } )
36+ . field ( "x" , Destruct . UInt16 )
37+ . field ( "y" , Destruct . UInt16 )
38+ . field ( "z" , Destruct . UInt16 )
39+ ) ;
4040
4141// structron
4242const PointsStruct = new Struct ( )
43- . addMember ( Struct . TYPES . UINT_LE , ' len' )
43+ . addMember ( Struct . TYPES . UINT_LE , " len" )
4444 . addArray (
45- new Struct ( ) . addMember ( Struct . TYPES . USHORT_LE , 'x' )
46- . addMember ( Struct . TYPES . USHORT_LE , 'y' )
47- . addMember ( Struct . TYPES . USHORT_LE , 'z' ) ,
48- 'points' , 0 , 'len'
45+ new Struct ( )
46+ . addMember ( Struct . TYPES . USHORT_LE , "x" )
47+ . addMember ( Struct . TYPES . USHORT_LE , "y" )
48+ . addMember ( Struct . TYPES . USHORT_LE , "z" ) ,
49+ "points" ,
50+ 0 ,
51+ "len"
4952 ) ;
5053
5154// Prepare input
@@ -54,29 +57,29 @@ var buf = Buffer.alloc(4 + n * 2 * 3);
5457
5558buf . writeUInt32LE ( n , 0 ) ;
5659for ( var i = 0 ; i < n ; i ++ ) {
57- buf . writeUInt16LE ( 123 , i * 6 + 0 + 4 ) ;
58- buf . writeUInt16LE ( 456 , i * 6 + 2 + 4 ) ;
59- buf . writeUInt16LE ( 789 , i * 6 + 4 + 4 ) ;
60+ buf . writeUInt16LE ( 123 , i * 6 + 0 + 4 ) ;
61+ buf . writeUInt16LE ( 456 , i * 6 + 2 + 4 ) ;
62+ buf . writeUInt16LE ( 789 , i * 6 + 4 + 4 ) ;
6063}
6164
6265// Run benchmarks
6366suite
64- . add ( ' binparse' , function ( ) {
65- const points = PointsParser . read ( buf ) ;
66- } )
67- . add ( ' binary-parser' , function ( ) {
68- const points = Points . parse ( buf ) ;
69- } )
70- . add ( ' destruct-js' , function ( ) {
71- const points = spec . read ( buf ) ;
72- } )
73- . add ( ' structron' , function ( ) {
74- const points = PointsStruct . read ( buf ) ;
75- } )
76- . on ( ' cycle' , function ( event ) {
67+ . add ( " binparse" , function ( ) {
68+ const points = PointsParser . read ( buf ) ;
69+ } )
70+ . add ( " binary-parser" , function ( ) {
71+ const points = Points . parse ( buf ) ;
72+ } )
73+ . add ( " destruct-js" , function ( ) {
74+ const points = spec . read ( buf ) ;
75+ } )
76+ . add ( " structron" , function ( ) {
77+ const points = PointsStruct . read ( buf ) ;
78+ } )
79+ . on ( " cycle" , function ( event ) {
7780 console . log ( String ( event . target ) ) ;
78- } )
79- . on ( ' complete' , function ( ) {
80- console . log ( ' Fastest is ' + this . filter ( ' fastest' ) . map ( ' name' ) ) ;
81- } )
82- . run ( { ' async' : true } ) ;
81+ } )
82+ . on ( " complete" , function ( ) {
83+ console . log ( " Fastest is " + this . filter ( " fastest" ) . map ( " name" ) ) ;
84+ } )
85+ . run ( { async : true } ) ;
0 commit comments