diff --git a/benchmark/buffers/buffer-write.js b/benchmark/buffers/buffer-write.js index 42ea9aa093cb88..297059c1926fc8 100644 --- a/benchmark/buffers/buffer-write.js +++ b/benchmark/buffers/buffer-write.js @@ -34,6 +34,7 @@ const INT32 = 0x7fffffff; const INT48 = 0x7fffffffffff; const UINT8 = 0xff; const UINT16 = 0xffff; +const UINT32 = 0xffffffff; const mod = { writeInt8: INT8, @@ -44,8 +45,8 @@ const mod = { writeUInt8: UINT8, writeUInt16BE: UINT16, writeUInt16LE: UINT16, - writeUInt32BE: INT32, - writeUInt32LE: INT32, + writeUInt32BE: UINT32, + writeUInt32LE: UINT32, writeUIntLE: INT8, writeUIntBE: INT16, writeIntLE: INT32, diff --git a/lib/internal/buffer.js b/lib/internal/buffer.js index fe10b20c80541b..2c65194bfa00cc 100644 --- a/lib/internal/buffer.js +++ b/lib/internal/buffer.js @@ -481,9 +481,12 @@ function writeU_Int48LE(buf, value, offset, min, max) { const newVal = Math.floor(value * 2 ** -32); buf[offset++] = value; - buf[offset++] = (value >>> 8); - buf[offset++] = (value >>> 16); - buf[offset++] = (value >>> 24); + value = value >>> 8; + buf[offset++] = value; + value = value >>> 8; + buf[offset++] = value; + value = value >>> 8; + buf[offset++] = value; buf[offset++] = newVal; buf[offset++] = (newVal >>> 8); return offset; @@ -495,9 +498,12 @@ function writeU_Int40LE(buf, value, offset, min, max) { const newVal = value; buf[offset++] = value; - buf[offset++] = (value >>> 8); - buf[offset++] = (value >>> 16); - buf[offset++] = (value >>> 24); + value = value >>> 8; + buf[offset++] = value; + value = value >>> 8; + buf[offset++] = value; + value = value >>> 8; + buf[offset++] = value; buf[offset++] = Math.floor(newVal * 2 ** -32); return offset; } @@ -507,9 +513,12 @@ function writeU_Int32LE(buf, value, offset, min, max) { checkInt(value, min, max, buf, offset, 3); buf[offset++] = value; - buf[offset++] = (value >>> 8); - buf[offset++] = (value >>> 16); - buf[offset++] = (value >>> 24); + value = value >>> 8; + buf[offset++] = value; + value = value >>> 8; + buf[offset++] = value; + value = value >>> 8; + buf[offset++] = value; return offset; } @@ -522,8 +531,10 @@ function writeU_Int24LE(buf, value, offset, min, max) { checkInt(value, min, max, buf, offset, 2); buf[offset++] = value; - buf[offset++] = (value >>> 8); - buf[offset++] = (value >>> 16); + value = value >>> 8; + buf[offset++] = value; + value = value >>> 8; + buf[offset++] = value; return offset; } @@ -582,11 +593,14 @@ function writeU_Int48BE(buf, value, offset, min, max) { const newVal = Math.floor(value * 2 ** -32); buf[offset++] = (newVal >>> 8); buf[offset++] = newVal; - buf[offset++] = (value >>> 24); - buf[offset++] = (value >>> 16); - buf[offset++] = (value >>> 8); - buf[offset++] = value; - return offset; + buf[offset + 3] = value; + value = value >>> 8; + buf[offset + 2] = value; + value = value >>> 8; + buf[offset + 1] = value; + value = value >>> 8; + buf[offset] = value; + return offset + 4; } function writeU_Int40BE(buf, value, offset, min, max) { @@ -594,22 +608,28 @@ function writeU_Int40BE(buf, value, offset, min, max) { checkInt(value, min, max, buf, offset, 4); buf[offset++] = Math.floor(value * 2 ** -32); - buf[offset++] = (value >>> 24); - buf[offset++] = (value >>> 16); - buf[offset++] = (value >>> 8); - buf[offset++] = value; - return offset; + buf[offset + 3] = value; + value = value >>> 8; + buf[offset + 2] = value; + value = value >>> 8; + buf[offset + 1] = value; + value = value >>> 8; + buf[offset] = value; + return offset + 4; } function writeU_Int32BE(buf, value, offset, min, max) { value = +value; checkInt(value, min, max, buf, offset, 3); - buf[offset++] = (value >>> 24); - buf[offset++] = (value >>> 16); - buf[offset++] = (value >>> 8); - buf[offset++] = value; - return offset; + buf[offset + 3] = value; + value = value >>> 8; + buf[offset + 2] = value; + value = value >>> 8; + buf[offset + 1] = value; + value = value >>> 8; + buf[offset] = value; + return offset + 4; } function writeUInt32BE(value, offset) { @@ -620,10 +640,12 @@ function writeU_Int24BE(buf, value, offset, min, max) { value = +value; checkInt(value, min, max, buf, offset, 2); - buf[offset++] = (value >>> 16); - buf[offset++] = (value >>> 8); - buf[offset++] = value; - return offset; + buf[offset + 2] = value; + value = value >>> 8; + buf[offset + 1] = value; + value = value >>> 8; + buf[offset] = value; + return offset + 3; } function writeU_Int16BE(buf, value, offset, min, max) {