Skip to content

lib: Enhancing Performance and Readability with Switch Statements 🚀 #51304

New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
240 changes: 128 additions & 112 deletions lib/internal/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,20 +170,22 @@ function readBigInt64BE(offset = 0) {
function readUIntLE(offset, byteLength) {
if (offset === undefined)
throw new ERR_INVALID_ARG_TYPE('offset', 'number', offset);
if (byteLength === 6)
return readUInt48LE(this, offset);
if (byteLength === 5)
return readUInt40LE(this, offset);
if (byteLength === 3)
return readUInt24LE(this, offset);
if (byteLength === 4)
return this.readUInt32LE(offset);
if (byteLength === 2)
return this.readUInt16LE(offset);
if (byteLength === 1)
return this.readUInt8(offset);

boundsError(byteLength, 6, 'byteLength');
switch (byteLength) {
case 6:
return readUInt48LE(this, offset);
case 5:
return readUInt40LE(this, offset);
case 3:
return readUInt24LE(this, offset);
case 4:
return this.readUInt32LE(offset);
case 2:
return this.readUInt16LE(offset);
case 1:
return this.readUInt8(offset);
default:
boundsError(byteLength, 6, 'byteLength');
}
}

function readUInt48LE(buf, offset = 0) {
Expand Down Expand Up @@ -259,20 +261,22 @@ function readUInt8(offset = 0) {
function readUIntBE(offset, byteLength) {
if (offset === undefined)
throw new ERR_INVALID_ARG_TYPE('offset', 'number', offset);
if (byteLength === 6)
return readUInt48BE(this, offset);
if (byteLength === 5)
return readUInt40BE(this, offset);
if (byteLength === 3)
return readUInt24BE(this, offset);
if (byteLength === 4)
return this.readUInt32BE(offset);
if (byteLength === 2)
return this.readUInt16BE(offset);
if (byteLength === 1)
return this.readUInt8(offset);

boundsError(byteLength, 6, 'byteLength');
switch (byteLength) {
case 6:
return readUInt48BE(this, offset);
case 5:
return readUInt40BE(this, offset);
case 3:
return readUInt24BE(this, offset);
case 4:
return this.readUInt32BE(offset);
case 2:
return this.readUInt16BE(offset);
case 1:
return this.readUInt8(offset);
default:
boundsError(byteLength, 6, 'byteLength');
}
}

function readUInt48BE(buf, offset = 0) {
Expand Down Expand Up @@ -339,20 +343,22 @@ function readUInt16BE(offset = 0) {
function readIntLE(offset, byteLength) {
if (offset === undefined)
throw new ERR_INVALID_ARG_TYPE('offset', 'number', offset);
if (byteLength === 6)
return readInt48LE(this, offset);
if (byteLength === 5)
return readInt40LE(this, offset);
if (byteLength === 3)
return readInt24LE(this, offset);
if (byteLength === 4)
return this.readInt32LE(offset);
if (byteLength === 2)
return this.readInt16LE(offset);
if (byteLength === 1)
return this.readInt8(offset);

boundsError(byteLength, 6, 'byteLength');
switch (byteLength) {
case 6:
return readInt48LE(this, offset);
case 5:
return readInt40LE(this, offset);
case 3:
return readInt24LE(this, offset);
case 4:
return this.readInt32LE(offset);
case 2:
return this.readInt16LE(offset);
case 1:
return this.readInt8(offset);
default:
boundsError(byteLength, 6, 'byteLength');
}
}

function readInt48LE(buf, offset = 0) {
Expand Down Expand Up @@ -431,20 +437,22 @@ function readInt8(offset = 0) {
function readIntBE(offset, byteLength) {
if (offset === undefined)
throw new ERR_INVALID_ARG_TYPE('offset', 'number', offset);
if (byteLength === 6)
return readInt48BE(this, offset);
if (byteLength === 5)
return readInt40BE(this, offset);
if (byteLength === 3)
return readInt24BE(this, offset);
if (byteLength === 4)
return this.readInt32BE(offset);
if (byteLength === 2)
return this.readInt16BE(offset);
if (byteLength === 1)
return this.readInt8(offset);

boundsError(byteLength, 6, 'byteLength');
switch (byteLength) {
case 6:
return readInt48BE(this, offset);
case 5:
return readInt40BE(this, offset);
case 3:
return readInt24BE(this, offset);
case 4:
return this.readInt32BE(offset);
case 2:
return this.readInt16BE(offset);
case 1:
return this.readInt8(offset);
default:
boundsError(byteLength, 6, 'byteLength');
}
}

function readInt48BE(buf, offset = 0) {
Expand Down Expand Up @@ -640,20 +648,22 @@ function writeBigInt64BE(value, offset = 0) {
}

function writeUIntLE(value, offset, byteLength) {
if (byteLength === 6)
return writeU_Int48LE(this, value, offset, 0, 0xffffffffffff);
if (byteLength === 5)
return writeU_Int40LE(this, value, offset, 0, 0xffffffffff);
if (byteLength === 3)
return writeU_Int24LE(this, value, offset, 0, 0xffffff);
if (byteLength === 4)
return writeU_Int32LE(this, value, offset, 0, 0xffffffff);
if (byteLength === 2)
return writeU_Int16LE(this, value, offset, 0, 0xffff);
if (byteLength === 1)
return writeU_Int8(this, value, offset, 0, 0xff);

boundsError(byteLength, 6, 'byteLength');
switch (byteLength) {
case 6:
return writeU_Int48LE(this, value, offset, 0, 0xffffffffffff);
case 5:
return writeU_Int40LE(this, value, offset, 0, 0xffffffffff);
case 3:
return writeU_Int24LE(this, value, offset, 0, 0xffffff);
case 4:
return writeU_Int32LE(this, value, offset, 0, 0xffffffff);
case 2:
return writeU_Int16LE(this, value, offset, 0, 0xffff);
case 1:
return writeU_Int8(this, value, offset, 0, 0xff);
default:
boundsError(byteLength, 6, 'byteLength');
}
}

function writeU_Int48LE(buf, value, offset, min, max) {
Expand Down Expand Up @@ -751,20 +761,22 @@ function writeUInt8(value, offset = 0) {
}

function writeUIntBE(value, offset, byteLength) {
if (byteLength === 6)
return writeU_Int48BE(this, value, offset, 0, 0xffffffffffff);
if (byteLength === 5)
return writeU_Int40BE(this, value, offset, 0, 0xffffffffff);
if (byteLength === 3)
return writeU_Int24BE(this, value, offset, 0, 0xffffff);
if (byteLength === 4)
return writeU_Int32BE(this, value, offset, 0, 0xffffffff);
if (byteLength === 2)
return writeU_Int16BE(this, value, offset, 0, 0xffff);
if (byteLength === 1)
return writeU_Int8(this, value, offset, 0, 0xff);

boundsError(byteLength, 6, 'byteLength');
switch (byteLength) {
case 6:
return writeU_Int48BE(this, value, offset, 0, 0xffffffffffff);
case 5:
return writeU_Int40BE(this, value, offset, 0, 0xffffffffff);
case 3:
return writeU_Int24BE(this, value, offset, 0, 0xffffff);
case 4:
return writeU_Int32BE(this, value, offset, 0, 0xffffffff);
case 2:
return writeU_Int16BE(this, value, offset, 0, 0xffff);
case 1:
return writeU_Int8(this, value, offset, 0, 0xff);
default:
boundsError(byteLength, 6, 'byteLength');
}
}

function writeU_Int48BE(buf, value, offset, min, max) {
Expand Down Expand Up @@ -843,20 +855,22 @@ function writeUInt16BE(value, offset = 0) {
}

function writeIntLE(value, offset, byteLength) {
if (byteLength === 6)
return writeU_Int48LE(this, value, offset, -0x800000000000, 0x7fffffffffff);
if (byteLength === 5)
return writeU_Int40LE(this, value, offset, -0x8000000000, 0x7fffffffff);
if (byteLength === 3)
return writeU_Int24LE(this, value, offset, -0x800000, 0x7fffff);
if (byteLength === 4)
return writeU_Int32LE(this, value, offset, -0x80000000, 0x7fffffff);
if (byteLength === 2)
return writeU_Int16LE(this, value, offset, -0x8000, 0x7fff);
if (byteLength === 1)
return writeU_Int8(this, value, offset, -0x80, 0x7f);

boundsError(byteLength, 6, 'byteLength');
switch (byteLength) {
case 6:
return writeU_Int48LE(this, value, offset, -0x800000000000, 0x7fffffffffff);
case 5:
return writeU_Int40LE(this, value, offset, -0x8000000000, 0x7fffffffff);
case 3:
return writeU_Int24LE(this, value, offset, -0x800000, 0x7fffff);
case 4:
return writeU_Int32LE(this, value, offset, -0x80000000, 0x7fffffff);
case 2:
return writeU_Int16LE(this, value, offset, -0x8000, 0x7fff);
case 1:
return writeU_Int8(this, value, offset, -0x80, 0x7f);
default:
boundsError(byteLength, 6, 'byteLength');
}
}

function writeInt32LE(value, offset = 0) {
Expand All @@ -872,20 +886,22 @@ function writeInt8(value, offset = 0) {
}

function writeIntBE(value, offset, byteLength) {
if (byteLength === 6)
return writeU_Int48BE(this, value, offset, -0x800000000000, 0x7fffffffffff);
if (byteLength === 5)
return writeU_Int40BE(this, value, offset, -0x8000000000, 0x7fffffffff);
if (byteLength === 3)
return writeU_Int24BE(this, value, offset, -0x800000, 0x7fffff);
if (byteLength === 4)
return writeU_Int32BE(this, value, offset, -0x80000000, 0x7fffffff);
if (byteLength === 2)
return writeU_Int16BE(this, value, offset, -0x8000, 0x7fff);
if (byteLength === 1)
return writeU_Int8(this, value, offset, -0x80, 0x7f);

boundsError(byteLength, 6, 'byteLength');
switch (byteLength) {
case 6:
return writeU_Int48BE(this, value, offset, -0x800000000000, 0x7fffffffffff);
case 5:
return writeU_Int40BE(this, value, offset, -0x8000000000, 0x7fffffffff);
case 4:
return writeU_Int32BE(this, value, offset, -0x80000000, 0x7fffffff);
case 3:
return writeU_Int24BE(this, value, offset, -0x800000, 0x7fffff);
case 2:
return writeU_Int16BE(this, value, offset, -0x8000, 0x7fff);
case 1:
return writeU_Int8(this, value, offset, -0x80, 0x7f);
default:
boundsError(byteLength, 6, 'byteLength');
}
}

function writeInt32BE(value, offset = 0) {
Expand Down
60 changes: 32 additions & 28 deletions lib/v8.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,37 +278,41 @@ Deserializer.prototype.readRawBytes = function readRawBytes(length) {

function arrayBufferViewTypeToIndex(abView) {
const type = ObjectPrototypeToString(abView);
if (type === '[object Int8Array]') return 0;
if (type === '[object Uint8Array]') return 1;
if (type === '[object Uint8ClampedArray]') return 2;
if (type === '[object Int16Array]') return 3;
if (type === '[object Uint16Array]') return 4;
if (type === '[object Int32Array]') return 5;
if (type === '[object Uint32Array]') return 6;
if (type === '[object Float32Array]') return 7;
if (type === '[object Float64Array]') return 8;
if (type === '[object DataView]') return 9;
// Index 10 is FastBuffer.
if (type === '[object BigInt64Array]') return 11;
if (type === '[object BigUint64Array]') return 12;
return -1;
switch (type) {
case '[object Int8Array]': return 0;
case '[object Uint8Array]': return 1;
case '[object Uint8ClampedArray]': return 2;
case '[object Int16Array]': return 3;
case '[object Uint16Array]': return 4;
case '[object Int32Array]': return 5;
case '[object Uint32Array]': return 6;
case '[object Float32Array]': return 7;
case '[object Float64Array]': return 8;
case '[object DataView]': return 9;
// Index 10 id FastBuffer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Index 10 id FastBuffer
// Index 10 is FastBuffer

case '[object BigInt64Array]': return 11;
case '[object BigUint64Array]': return 12;
default: return -1;
}
}

function arrayBufferViewIndexToType(index) {
if (index === 0) return Int8Array;
if (index === 1) return Uint8Array;
if (index === 2) return Uint8ClampedArray;
if (index === 3) return Int16Array;
if (index === 4) return Uint16Array;
if (index === 5) return Int32Array;
if (index === 6) return Uint32Array;
if (index === 7) return Float32Array;
if (index === 8) return Float64Array;
if (index === 9) return DataView;
if (index === 10) return FastBuffer;
if (index === 11) return BigInt64Array;
if (index === 12) return BigUint64Array;
return undefined;
switch (index) {
case 0: return Int8Array;
case 1: return Uint8Array;
case 2: return Uint8ClampedArray;
case 3: return Int16Array;
case 4: return Uint16Array;
case 5: return Int32Array;
case 6: return Uint32Array;
case 7: return Float32Array;
case 8: return Float64Array;
case 9: return DataView;
case 10: return FastBuffer;
case 11: return BigInt64Array;
case 12: return BigUint64Array;
default: return undefined;
}
}

class DefaultSerializer extends Serializer {
Expand Down