Replace some assert usage with unreachable in the src/shared/util.js file

Inlining the checks should be a *tiny bit* more efficient, since it avoids have to make *unconditional* function calls in these fairly commonly used helper functions.
This commit is contained in:
Jonas Jenwald 2022-01-15 12:45:27 +01:00
parent e0032811cd
commit 0e1b93bf20

View File

@ -576,10 +576,13 @@ class AbortException extends BaseException {
} }
function bytesToString(bytes) { function bytesToString(bytes) {
assert( if (
bytes !== null && typeof bytes === "object" && bytes.length !== undefined, typeof bytes !== "object" ||
"Invalid argument for bytesToString" bytes === null ||
); bytes.length === undefined
) {
unreachable("Invalid argument for bytesToString");
}
const length = bytes.length; const length = bytes.length;
const MAX_ARGUMENT_COUNT = 8192; const MAX_ARGUMENT_COUNT = 8192;
if (length < MAX_ARGUMENT_COUNT) { if (length < MAX_ARGUMENT_COUNT) {
@ -595,7 +598,9 @@ function bytesToString(bytes) {
} }
function stringToBytes(str) { function stringToBytes(str) {
assert(typeof str === "string", "Invalid argument for stringToBytes"); if (typeof str !== "string") {
unreachable("Invalid argument for stringToBytes");
}
const length = str.length; const length = str.length;
const bytes = new Uint8Array(length); const bytes = new Uint8Array(length);
for (let i = 0; i < length; ++i) { for (let i = 0; i < length; ++i) {
@ -609,12 +614,15 @@ function stringToBytes(str) {
* @param {Array<any>|Uint8Array|string} arr * @param {Array<any>|Uint8Array|string} arr
* @returns {number} * @returns {number}
*/ */
// eslint-disable-next-line consistent-return
function arrayByteLength(arr) { function arrayByteLength(arr) {
if (arr.length !== undefined) { if (arr.length !== undefined) {
return arr.length; return arr.length;
} }
assert(arr.byteLength !== undefined, "arrayByteLength - invalid argument."); if (arr.byteLength !== undefined) {
return arr.byteLength; return arr.byteLength;
}
unreachable("Invalid argument for arrayByteLength");
} }
/** /**