Created
June 26, 2011 00:24
-
-
Save bnoordhuis/1047078 to your computer and use it in GitHub Desktop.
Buffer.writeUInt32() benchmarklet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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