Skip to content

Instantly share code, notes, and snippets.

@bnoordhuis
Created June 26, 2011 00:24
Show Gist options
  • Save bnoordhuis/1047078 to your computer and use it in GitHub Desktop.
Save bnoordhuis/1047078 to your computer and use it in GitHub Desktop.
Buffer.writeUInt32() benchmarklet
$ time ./node tmp/test-uint32-slow.js
real 0m8.819s
user 0m8.790s
sys 0m0.030s
$ time ./node tmp/test-uint32-fast.js
real 0m2.210s
user 0m2.190s
sys 0m0.030s
Buffer.prototype.fastWriteUInt32 = function(value, offset, be) {
if (be) {
this[offset] = (value >>> 24) & 0xFF;
this[offset + 1] = (value >>> 16) & 0xFF;
this[offset + 2] = (value >>> 8) & 0xFF;
this[offset + 3] = value & 0xFF;
}
else {
this[offset] = value & 0xFF;
this[offset + 1] = (value >>> 8) & 0xFF;
this[offset + 2] = (value >>> 16) & 0xFF;
this[offset + 3] = (value >>> 24) & 0xFF;
}
};
var b = new Buffer(4);
for (var i = 0; i < 32 * 1024 * 1024; ++i) {
b.fastWriteUInt32(i, 0, false);
}
var b = new Buffer(4);
for (var i = 0; i < 32 * 1024 * 1024; ++i) {
b.writeUInt32(i, 0, 'little');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment