Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Add a return value to Buffer.write* methods that returns the new total offset after the write is complete #3624

Closed
wants to merge 2 commits into
from
Jump to file or symbol
Failed to load files and symbols.
+58 −12
Split
@@ -0,0 +1,36 @@
+function testBufferWithReturn(){
+ var buffer = new Buffer(50);
+ var offset = 0;
+
+ console.time('buffer with return x 100K');
+
+ for(var i = 0; i < 100000; i++){
+ offset = 0;
+
@bnoordhuis

bnoordhuis Aug 28, 2012

Member

Too much whitespace, remove the newlines.

+ offset = buffer.writeUInt8(1, offset);
+
+ offset = buffer.writeUInt16BE(2, offset);
+
+ offset = buffer.writeUInt32BE(3, offset);
+
+ offset = buffer.writeInt8(-1, offset);
+
+ offset = buffer.writeInt16BE(-2, offset);
+
+ offset = buffer.writeInt32BE(-3, offset);
+
+ offset = buffer.writeFloatBE(1.234, offset);
+
+ offset = buffer.writeDoubleBE(1.234, offset);
+ }
+
+ console.timeEnd('buffer with return x 100K');
+}
+
+console.time('buffer with return x 1 Million');
+
+for(var x=0; x<10; x++){
@bnoordhuis

bnoordhuis Aug 28, 2012

Member

Style: spaces between operators.

+ testBufferWithReturn();
+}
+
+console.timeEnd('buffer with return x 1 Million');
View
@@ -876,6 +876,7 @@ Buffer.prototype.writeUInt8 = function(value, offset, noAssert) {
}
buffer[offset] = value;
+ return offset + 1;
};
function writeUInt16(buffer, value, offset, isBigEndian, noAssert) {
@@ -902,14 +903,15 @@ function writeUInt16(buffer, value, offset, isBigEndian, noAssert) {
buffer[offset + 1] = (value & 0xff00) >>> 8;
buffer[offset] = value & 0x00ff;
}
+ return offset + 2;
}
Buffer.prototype.writeUInt16LE = function(value, offset, noAssert) {
- writeUInt16(this, value, offset, false, noAssert);
+ return writeUInt16(this, value, offset, false, noAssert);
};
Buffer.prototype.writeUInt16BE = function(value, offset, noAssert) {
- writeUInt16(this, value, offset, true, noAssert);
+ return writeUInt16(this, value, offset, true, noAssert);
};
function writeUInt32(buffer, value, offset, isBigEndian, noAssert) {
@@ -940,14 +942,15 @@ function writeUInt32(buffer, value, offset, isBigEndian, noAssert) {
buffer[offset + 1] = (value >>> 8) & 0xff;
buffer[offset] = value & 0xff;
}
+ return offset + 4;
}
Buffer.prototype.writeUInt32LE = function(value, offset, noAssert) {
- writeUInt32(this, value, offset, false, noAssert);
+ return writeUInt32(this, value, offset, false, noAssert);
};
Buffer.prototype.writeUInt32BE = function(value, offset, noAssert) {
- writeUInt32(this, value, offset, true, noAssert);
+ return writeUInt32(this, value, offset, true, noAssert);
};
@@ -1032,6 +1035,7 @@ Buffer.prototype.writeInt8 = function(value, offset, noAssert) {
} else {
buffer.writeUInt8(0xff + value + 1, offset, noAssert);
}
+ return offset + 1;
};
function writeInt16(buffer, value, offset, isBigEndian, noAssert) {
@@ -1056,14 +1060,15 @@ function writeInt16(buffer, value, offset, isBigEndian, noAssert) {
} else {
writeUInt16(buffer, 0xffff + value + 1, offset, isBigEndian, noAssert);
}
+ return offset + 2;
}
Buffer.prototype.writeInt16LE = function(value, offset, noAssert) {
- writeInt16(this, value, offset, false, noAssert);
+ return writeInt16(this, value, offset, false, noAssert);
};
Buffer.prototype.writeInt16BE = function(value, offset, noAssert) {
- writeInt16(this, value, offset, true, noAssert);
+ return writeInt16(this, value, offset, true, noAssert);
};
function writeInt32(buffer, value, offset, isBigEndian, noAssert) {
@@ -1088,14 +1093,15 @@ function writeInt32(buffer, value, offset, isBigEndian, noAssert) {
} else {
writeUInt32(buffer, 0xffffffff + value + 1, offset, isBigEndian, noAssert);
}
+ return offset + 4;
}
Buffer.prototype.writeInt32LE = function(value, offset, noAssert) {
- writeInt32(this, value, offset, false, noAssert);
+ return writeInt32(this, value, offset, false, noAssert);
};
Buffer.prototype.writeInt32BE = function(value, offset, noAssert) {
- writeInt32(this, value, offset, true, noAssert);
+ return writeInt32(this, value, offset, true, noAssert);
};
function writeFloat(buffer, value, offset, isBigEndian, noAssert) {
@@ -1117,14 +1123,16 @@ function writeFloat(buffer, value, offset, isBigEndian, noAssert) {
require('buffer_ieee754').writeIEEE754(buffer, value, offset, isBigEndian,
23, 4);
+
+ return offset + 4;
}
Buffer.prototype.writeFloatLE = function(value, offset, noAssert) {
- writeFloat(this, value, offset, false, noAssert);
+ return writeFloat(this, value, offset, false, noAssert);
};
Buffer.prototype.writeFloatBE = function(value, offset, noAssert) {
- writeFloat(this, value, offset, true, noAssert);
+ return writeFloat(this, value, offset, true, noAssert);
};
function writeDouble(buffer, value, offset, isBigEndian, noAssert) {
@@ -1146,12 +1154,14 @@ function writeDouble(buffer, value, offset, isBigEndian, noAssert) {
require('buffer_ieee754').writeIEEE754(buffer, value, offset, isBigEndian,
52, 8);
+
+ return offset + 8;
}
Buffer.prototype.writeDoubleLE = function(value, offset, noAssert) {
- writeDouble(this, value, offset, false, noAssert);
+ return writeDouble(this, value, offset, false, noAssert);
};
Buffer.prototype.writeDoubleBE = function(value, offset, noAssert) {
- writeDouble(this, value, offset, true, noAssert);
+ return writeDouble(this, value, offset, true, noAssert);
};