faster strings for bufferwriter.

This commit is contained in:
Christopher Jeffrey 2016-04-29 17:09:13 -07:00
parent 5ef65a4129
commit 383014e754
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -56,6 +56,7 @@ BufferWriter.prototype.render = function render(keep) {
case '64be': off += utils.write64BE(data, item[1], off); break;
case 'varint': off += utils.writeVarint(data, item[1], off); break;
case 'bytes': off += item[1].copy(data, off); break;
case 'str': off += data.write(item[1], off, item[2]); break;
case 'checksum':
off += utils.checksum(data.slice(0, off)).copy(data, off);
break;
@ -244,9 +245,10 @@ BufferWriter.prototype.getSize = function getSize() {
*/
BufferWriter.prototype.writeString = function writeString(value, enc) {
if (typeof value === 'string')
value = new Buffer(value, enc);
this.writeBytes(value);
if (typeof value !== 'string')
return this.writeBytes(value);
this.written += Buffer.byteLength(value, enc);
this.data.push(['str', value, enc]);
};
/**
@ -255,9 +257,7 @@ BufferWriter.prototype.writeString = function writeString(value, enc) {
*/
BufferWriter.prototype.writeHash = function writeHash(value) {
if (typeof value === 'string')
value = new Buffer(value, 'hex');
this.writeBytes(value);
this.writeString(value, 'hex');
};
/**
@ -267,9 +267,18 @@ BufferWriter.prototype.writeHash = function writeHash(value) {
*/
BufferWriter.prototype.writeVarString = function writeVarString(value, enc) {
if (typeof value === 'string')
value = new Buffer(value, enc);
this.writeVarBytes(value);
var size;
if (typeof value !== 'string')
return this.writeVarBytes(value);
size = Buffer.byteLength(value, enc);
this.written += utils.sizeVarint(size);
this.written += size;
this.data.push(['varint', size]);
this.data.push(['str', value, enc]);
};
/**