Skip to content

Instantly share code, notes, and snippets.

@rootslab
Last active August 29, 2015 14:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rootslab/01c7a3d4bca97c85d078 to your computer and use it in GitHub Desktop.
Save rootslab/01c7a3d4bca97c85d078 to your computer and use it in GitHub Desktop.
Buffer.prototype.readUInt24LE = function(offset, noAssert) {
offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 3, this.length);
return ((this[offset]) |
(this[offset + 1] << 8) |
(this[offset + 2] << 16));
};
Buffer.prototype.readUInt24BE = function(offset, noAssert) {
offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 3, this.length);
return ((this[offset] << 16) |
(this[offset + 1] << 8) |
this[offset + 2]);
};
Buffer.prototype.readInt24LE = function(offset, noAssert) {
offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 3, this.length);
return (this[offset]) |
(this[offset + 1] << 8) |
(this[offset + 2] << 16);
};
Buffer.prototype.readInt24BE = function(offset, noAssert) {
offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 3, this.length);
return (this[offset] << 16) |
(this[offset + 1] << 8) |
(this[offset + 2]);
};
Buffer.prototype.writeUInt24LE = function(value, offset, noAssert) {
value = +value;
offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 3, 0xffffff, 0);
this[offset + 2] = (value >>> 16);
this[offset + 1] = (value >>> 8);
this[offset] = value;
return offset + 3;
};
Buffer.prototype.writeUInt24BE = function(value, offset, noAssert) {
value = +value;
offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 3, 0xffffff, 0);
this[offset] = (value >>> 16);
this[offset + 1] = (value >>> 8);
this[offset + 2] = value;
return offset + 3;
};
Buffer.prototype.writeInt24LE = function(value, offset, noAssert) {
value = +value;
offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 3, 0x7fffff, -0x800000);
this[offset] = value;
this[offset + 1] = (value >>> 8);
this[offset + 2] = (value >>> 16);
return offset + 3;
};
Buffer.prototype.writeInt24BE = function(value, offset, noAssert) {
value = +value;
offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 3, 0x7fffff, -0x800000);
this[offset] = (value >>> 16);
this[offset + 1] = (value >>> 8);
this[offset + 2] = value;
return offset + 3;
};
var log = console.log
, Kombo = require( '../' )
, test = function ( mbytes ) {
var mb = Math.max( mbytes, 1 )
, b = new Buffer( mb * 1024 * 1024 )
, stime = -1
, etime= -1
, i = 0
;
log( '\nfilling test buffer (%d MB)..', mb );
for ( ; i < b.length; ++i ) b[ i ] = i;
stime = Date.now();
for ( i = 0; i < b.length - 3; i+= 3 ) b.readUIntBE( i, 3 );
etime = Date.now();
log( '- readUIntBE: %d s', ( etime - stime ) / 1000 );
stime = Date.now();
for ( i = 0; i < b.length - 3; i+= 3 ) b.readUInt24BE( i );
etime = Date.now();
log( '- readUInt24BE: %d s', ( etime - stime ) / 1000 )
stime = Date.now();
for ( i = 0; i < b.length - 3; i+= 3 ) b.writeUIntBE( 0xffffff, i, 3 );
etime = Date.now();
log( '- writeUIntBE: %d s', ( etime - stime ) / 1000 );
stime = Date.now();
for ( i = 0; i < b.length - 3; i+= 3 ) b.writeUInt24BE( 0xffffff, i );
etime = Date.now();
log( '- writeUInt24BE: %d s', ( etime - stime ) / 1000 );
};
test( 1 );
test( 8 );
test( 16 );
test( 32 );
test( 64 );
test( 128 );
log();
@rootslab
Copy link
Author

output results:

filling test buffer (1 MB)..

  • readUIntBE: 0.013 s
  • readUInt24BE: 0.01 s
  • writeUIntBE: 0.022 s
  • writeUInt24BE: 0.014 s

filling test buffer (8 MB)..

  • readUIntBE: 0.085 s
  • readUInt24BE: 0.061 s
  • writeUIntBE: 0.145 s
  • writeUInt24BE: 0.095 s

filling test buffer (16 MB)..

  • readUIntBE: 0.176 s
  • readUInt24BE: 0.125 s
  • writeUIntBE: 0.294 s
  • writeUInt24BE: 0.176 s

filling test buffer (32 MB)..

  • readUIntBE: 0.34 s
  • readUInt24BE: 0.247 s
  • writeUIntBE: 0.566 s
  • writeUInt24BE: 0.349 s

filling test buffer (64 MB)..

  • readUIntBE: 0.674 s
  • readUInt24BE: 0.49 s
  • writeUIntBE: 1.126 s
  • writeUInt24BE: 0.698 s

filling test buffer (128 MB)..

  • readUIntBE: 1.35 s
  • readUInt24BE: 0.98 s
  • writeUIntBE: 2.25 s
  • writeUInt24BE: 1.396 s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment