From d8dfd7a9703937e9574a4529927b0d7dd2ca39b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Sun, 2 Mar 2025 01:29:17 +0100 Subject: [PATCH] buffer: optimize read UInt16/24 functions with bitwise operations --- lib/internal/buffer.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/internal/buffer.js b/lib/internal/buffer.js index dc21fb3e334588..7dec60f551fbdf 100644 --- a/lib/internal/buffer.js +++ b/lib/internal/buffer.js @@ -234,7 +234,7 @@ function readUInt24LE(buf, offset = 0) { if (first === undefined || last === undefined) boundsError(offset, buf.length - 3); - return first + buf[++offset] * 2 ** 8 + last * 2 ** 16; + return first | (buf[++offset] << 8) | (last << 16); } function readUInt16LE(offset = 0) { @@ -244,7 +244,7 @@ function readUInt16LE(offset = 0) { if (first === undefined || last === undefined) boundsError(offset, this.length - 2); - return first + last * 2 ** 8; + return first | (last << 8); } function readUInt8(offset = 0) { @@ -323,7 +323,7 @@ function readUInt24BE(buf, offset = 0) { if (first === undefined || last === undefined) boundsError(offset, buf.length - 3); - return first * 2 ** 16 + buf[++offset] * 2 ** 8 + last; + return (first << 16) | (buf[++offset] << 8) | last; } function readUInt16BE(offset = 0) { @@ -333,7 +333,7 @@ function readUInt16BE(offset = 0) { if (first === undefined || last === undefined) boundsError(offset, this.length - 2); - return first * 2 ** 8 + last; + return (first << 8) | last; } function readIntLE(offset, byteLength) {