keichi / binary-parser

A blazing-fast declarative parser builder for binary data
MIT License
857 stars 133 forks source link

Use DataView instead of Buffer in hand-written benchmark #249

Closed screeny05 closed 9 months ago

screeny05 commented 9 months ago

This will make the benchmark a lot fairer as DataView is pretty optimized in node.js and also because binary-parser itself uses DataView instead of Buffer. Also i upgraded to binparse@2.

Example Results on my machine (Macbook M1 MacOS 14.1).

Before this change:

❯ node bench.js
hand-written x 51,715 ops/sec ±0.35% (100 runs sampled)
binparse x 19,540 ops/sec ±0.58% (96 runs sampled)
binary-parser x 149,758 ops/sec ±0.58% (96 runs sampled)
destruct-js x 843 ops/sec ±0.17% (99 runs sampled)
structron x 7,554 ops/sec ±0.55% (97 runs sampled)
Fastest is binary-parser

After this change:

❯ node bench.js
hand-written x 153,338 ops/sec ±1.84% (95 runs sampled)
binparse x 136,028 ops/sec ±3.38% (92 runs sampled)
binary-parser x 153,768 ops/sec ±0.70% (91 runs sampled)
destruct-js x 844 ops/sec ±0.32% (99 runs sampled)
structron x 7,697 ops/sec ±0.31% (100 runs sampled)
Fastest is hand-written

That's more than tripple the speed for the hand-written implementation than before.

keichi commented 9 months ago

Thanks for pointing this out! I actually wasn't aware of the performance difference between DataView and Buffer. We initially used Buffer but then switched to DataView to support browser environments (https://github.com/keichi/binary-parser/pull/140)

screeny05 commented 9 months ago

Sure!

I wasn't aware of this as well until i read this article by the v8 dev team:

https://v8.dev/blog/dataview