diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2023-06-05 10:52:40 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-05 10:52:40 +0200 |
commit | 21c2c01ebed902c70763bb9319c3ec48c4cb5284 (patch) | |
tree | 82f01c418fc9abfb39aa370b3f3a6af820b10abb | |
parent | adf41edda12a26a84cb8b4252404aae2a9e7ae03 (diff) |
perf: optimize RegExp usage in JS (#19364)
Towards https://github.com/denoland/deno/issues/19330
Shows about 1% improvement in the HTTP benchmark.
-rw-r--r-- | ext/fetch/20_headers.js | 16 | ||||
-rw-r--r-- | ext/fetch/23_request.js | 4 | ||||
-rw-r--r-- | ext/fetch/23_response.js | 4 | ||||
-rw-r--r-- | ext/web/01_mimesniff.js | 3 | ||||
-rw-r--r-- | ext/webidl/00_webidl.js | 3 | ||||
-rw-r--r-- | 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 89b9e1a2b..6d934a7c1 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 4c46ebe75..daf77a834 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 52ebc91fe..dc4e75434 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 ad89f33cd..7d402e080 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 dfaa774e2..ca1c7c606 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 f7dd516ff..c4c686b9c 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( |