From ca625dcd1d373c33756be4df1c273315b6219139 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Mon, 5 Jun 2023 10:52:40 +0200 Subject: [PATCH] perf: optimize RegExp usage in JS (#19364) Towards https://github.com/denoland/deno/issues/19330 Shows about 1% improvement in the HTTP benchmark. --- ext/fetch/20_headers.js | 16 ++++++++-------- ext/fetch/23_request.js | 4 ++-- ext/fetch/23_response.js | 4 ++-- ext/web/01_mimesniff.js | 3 ++- ext/webidl/00_webidl.js | 3 ++- ext/websocket/01_websocket.js | 5 +++-- 6 files changed, 19 insertions(+), 16 deletions(-) diff --git a/ext/fetch/20_headers.js b/ext/fetch/20_headers.js index 89b9e1a2bc..6d934a7c1c 100644 --- a/ext/fetch/20_headers.js +++ b/ext/fetch/20_headers.js @@ -30,7 +30,7 @@ const { ArrayPrototypeFilter, ObjectEntries, ObjectHasOwn, - RegExpPrototypeTest, + RegExpPrototypeExec, SafeArrayIterator, SafeRegExp, Symbol, @@ -102,10 +102,10 @@ function appendHeader(headers, name, value) { value = normalizeHeaderValue(value); // 2. - if (!RegExpPrototypeTest(HTTP_TOKEN_CODE_POINT_RE, name)) { + if (RegExpPrototypeExec(HTTP_TOKEN_CODE_POINT_RE, name) === null) { throw new TypeError("Header name is not valid."); } - if (RegExpPrototypeTest(ILLEGAL_VALUE_CHARS, value)) { + if (RegExpPrototypeExec(ILLEGAL_VALUE_CHARS, value) !== null) { throw new TypeError("Header value is not valid."); } @@ -282,7 +282,7 @@ class Headers { webidl.requiredArguments(arguments.length, 1, prefix); name = webidl.converters["ByteString"](name, prefix, "Argument 1"); - if (!RegExpPrototypeTest(HTTP_TOKEN_CODE_POINT_RE, name)) { + if (RegExpPrototypeExec(HTTP_TOKEN_CODE_POINT_RE, name) === null) { throw new TypeError("Header name is not valid."); } if (this[_guard] == "immutable") { @@ -307,7 +307,7 @@ class Headers { webidl.requiredArguments(arguments.length, 1, prefix); name = webidl.converters["ByteString"](name, prefix, "Argument 1"); - if (!RegExpPrototypeTest(HTTP_TOKEN_CODE_POINT_RE, name)) { + if (RegExpPrototypeExec(HTTP_TOKEN_CODE_POINT_RE, name) === null) { throw new TypeError("Header name is not valid."); } @@ -323,7 +323,7 @@ class Headers { webidl.requiredArguments(arguments.length, 1, prefix); name = webidl.converters["ByteString"](name, prefix, "Argument 1"); - if (!RegExpPrototypeTest(HTTP_TOKEN_CODE_POINT_RE, name)) { + if (RegExpPrototypeExec(HTTP_TOKEN_CODE_POINT_RE, name) === null) { throw new TypeError("Header name is not valid."); } @@ -351,10 +351,10 @@ class Headers { value = normalizeHeaderValue(value); // 2. - if (!RegExpPrototypeTest(HTTP_TOKEN_CODE_POINT_RE, name)) { + if (RegExpPrototypeExec(HTTP_TOKEN_CODE_POINT_RE, name) === null) { throw new TypeError("Header name is not valid."); } - if (RegExpPrototypeTest(ILLEGAL_VALUE_CHARS, value)) { + if (RegExpPrototypeExec(ILLEGAL_VALUE_CHARS, value) !== null) { throw new TypeError("Header value is not valid."); } diff --git a/ext/fetch/23_request.js b/ext/fetch/23_request.js index 4c46ebe750..daf77a834e 100644 --- a/ext/fetch/23_request.js +++ b/ext/fetch/23_request.js @@ -36,7 +36,7 @@ const { ArrayPrototypeSplice, ObjectKeys, ObjectPrototypeIsPrototypeOf, - RegExpPrototypeTest, + RegExpPrototypeExec, StringPrototypeStartsWith, Symbol, SymbolFor, @@ -227,7 +227,7 @@ function validateAndNormalizeMethod(m) { } // Regular path - if (!RegExpPrototypeTest(HTTP_TOKEN_CODE_POINT_RE, m)) { + if (RegExpPrototypeExec(HTTP_TOKEN_CODE_POINT_RE, m) === null) { throw new TypeError("Method is not valid."); } const upperCase = byteUpperCase(m); diff --git a/ext/fetch/23_response.js b/ext/fetch/23_response.js index 52ebc91fe6..dc4e754342 100644 --- a/ext/fetch/23_response.js +++ b/ext/fetch/23_response.js @@ -37,7 +37,7 @@ const { ObjectDefineProperties, ObjectPrototypeIsPrototypeOf, RangeError, - RegExpPrototypeTest, + RegExpPrototypeExec, SafeArrayIterator, SafeRegExp, Symbol, @@ -179,7 +179,7 @@ function initializeAResponse(response, init, bodyWithType) { // 2. if ( init.statusText && - !RegExpPrototypeTest(REASON_PHRASE_RE, init.statusText) + RegExpPrototypeExec(REASON_PHRASE_RE, init.statusText) === null ) { throw new TypeError("Status text is not valid."); } diff --git a/ext/web/01_mimesniff.js b/ext/web/01_mimesniff.js index ad89f33cd7..7d402e0801 100644 --- a/ext/web/01_mimesniff.js +++ b/ext/web/01_mimesniff.js @@ -13,6 +13,7 @@ const { MapPrototypeHas, MapPrototypeSet, RegExpPrototypeTest, + RegExpPrototypeExec, SafeMap, SafeMapIterator, StringPrototypeReplaceAll, @@ -197,7 +198,7 @@ function serializeMimeType(mimeType) { for (const param of new SafeMapIterator(mimeType.parameters)) { serialization += `;${param[0]}=`; let value = param[1]; - if (!RegExpPrototypeTest(HTTP_TOKEN_CODE_POINT_RE, value)) { + if (RegExpPrototypeExec(HTTP_TOKEN_CODE_POINT_RE, value) === null) { value = StringPrototypeReplaceAll(value, "\\", "\\\\"); value = StringPrototypeReplaceAll(value, '"', '\\"'); value = `"${value}"`; diff --git a/ext/webidl/00_webidl.js b/ext/webidl/00_webidl.js index dfaa774e29..ca1c7c6064 100644 --- a/ext/webidl/00_webidl.js +++ b/ext/webidl/00_webidl.js @@ -59,6 +59,7 @@ const { ReflectHas, ReflectOwnKeys, RegExpPrototypeTest, + RegExpPrototypeExec, SafeRegExp, SafeSet, SetPrototypeEntries, @@ -406,7 +407,7 @@ converters.DOMString = function (V, prefix, context, opts = {}) { const IS_BYTE_STRING = new SafeRegExp(/^[\x00-\xFF]*$/); converters.ByteString = (V, prefix, context, opts) => { const x = converters.DOMString(V, prefix, context, opts); - if (!RegExpPrototypeTest(IS_BYTE_STRING, x)) { + if (RegExpPrototypeExec(IS_BYTE_STRING, x) === null) { throw makeException( TypeError, "is not a valid ByteString", diff --git a/ext/websocket/01_websocket.js b/ext/websocket/01_websocket.js index f7dd516ff0..c4c686b9c7 100644 --- a/ext/websocket/01_websocket.js +++ b/ext/websocket/01_websocket.js @@ -33,7 +33,7 @@ const { ObjectDefineProperties, ObjectPrototypeIsPrototypeOf, PromisePrototypeThen, - RegExpPrototypeTest, + RegExpPrototypeExec, SafeSet, SetPrototypeGetSize, // TODO(lucacasonato): add SharedArrayBuffer to primordials @@ -256,7 +256,8 @@ class WebSocket extends EventTarget { if ( ArrayPrototypeSome( protocols, - (protocol) => !RegExpPrototypeTest(HTTP_TOKEN_CODE_POINT_RE, protocol), + (protocol) => + RegExpPrototypeExec(HTTP_TOKEN_CODE_POINT_RE, protocol) === null, ) ) { throw new DOMException(