From 9f26e639dd6ddc9416bfd4d12b87efdac2699b1d Mon Sep 17 00:00:00 2001 From: Aaron O'Mullan Date: Tue, 13 Apr 2021 02:45:57 +0200 Subject: [PATCH] perf(fetch): optimize normalizeMethod() (#10154) --- op_crates/fetch/26_fetch.js | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/op_crates/fetch/26_fetch.js b/op_crates/fetch/26_fetch.js index 8dc7c2056c..0bcd8f85ca 100644 --- a/op_crates/fetch/26_fetch.js +++ b/op_crates/fetch/26_fetch.js @@ -922,22 +922,36 @@ }); } + /** + * @param {string} m + * @returns {boolean} + */ + function isKnownMethod(m) { + return ( + m === "DELETE" || + m === "GET" || + m === "HEAD" || + m === "OPTIONS" || + m === "POST" || + m === "PUT" + ); + } + /** * @param {string} m * @returns {string} */ function normalizeMethod(m) { + // Fast path for already valid methods + if (isKnownMethod(m)) { + return m; + } + // Normalize lower case (slowpath and should be avoided ...) const u = byteUpperCase(m); - if ( - u === "DELETE" || - u === "GET" || - u === "HEAD" || - u === "OPTIONS" || - u === "POST" || - u === "PUT" - ) { + if (isKnownMethod(u)) { return u; } + // Otherwise passthrough return m; }