bitparser
Version:
Optimized parsing of bits from a Buffer
173 lines (162 loc) • 3.33 kB
JavaScript
var assert = require('assert');
var bitparser = require('../bitparser');
function bench(label, fn) {
var buf = new Buffer([0xDE, 0xAD, 0xBE, 0xEF, 0xF0, 0xAA, 0x55, 0x00, 0x0F]);
var start = process.hrtime();
for (var i=0; i<100000; i++) {
fn(buf);
}
var diff = process.hrtime(start);
console.log('benchmark "%s" took %d msec', label, diff[0] * 1e3 + diff[1]/1E6);
}
bench('readUint8', function(buf) {
buf.readUInt8(0);
buf.readUInt8(1);
buf.readUInt8(2);
buf.readUInt8(3);
buf.readUInt8(4);
buf.readUInt8(5);
buf.readUInt8(6);
buf.readUInt8(7);
buf.readUInt8(8);
});
bench('readUint8+noAssert', function(buf) {
buf.readUInt8(0, true);
buf.readUInt8(1, true);
buf.readUInt8(2, true);
buf.readUInt8(3, true);
buf.readUInt8(4, true);
buf.readUInt8(5, true);
buf.readUInt8(6, true);
buf.readUInt8(7, true);
buf.readUInt8(8, true);
});
bench('readUint32', function(buf) {
buf.readUInt32BE(0, true);
buf.readUInt32BE(1, true);
buf.readUInt32BE(2, true);
buf.readUInt32BE(3, true);
buf.readUInt32BE(4, true);
buf.readUInt32BE(5, true);
buf.readUInt16BE(6, true);
buf.readUInt16BE(7, true);
buf.readUInt8(8, true);
});
bench('bitparse8', function(buf) {
var bp = bitparser(buf);
bp.readBits(8);
bp.readBits(8);
bp.readBits(8);
bp.readBits(8);
bp.readBits(8);
bp.readBits(8);
bp.readBits(8);
bp.readBits(8);
bp.readBits(8);
});
bench('bitparse4', function(buf) {
var bp = bitparser(buf);
bp.readBits(4);
bp.readBits(4);
bp.readBits(4);
bp.readBits(4);
bp.readBits(4);
bp.readBits(4);
bp.readBits(4);
bp.readBits(4);
bp.readBits(4);
bp.readBits(4);
bp.readBits(4);
bp.readBits(4);
bp.readBits(4);
bp.readBits(4);
bp.readBits(4);
bp.readBits(4);
bp.readBits(4);
bp.readBits(4);
});
bench('bitparse1', function(buf) {
var bp = bitparser(buf);
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
bp.read1Bit();
});
bench('bitparse1-loop', function(buf) {
var bp = bitparser(buf);
for (i=0; i<8*9; i++)
bp.read1Bit();
});
bench('bitparse1-alt-loop', function(buf) {
var bp = bitparser(buf);
for (i=0; i<8*9; i++)
bp.readBits(1);
});