perf: optimize RegExp usage in JS (#19364)

Towards https://github.com/denoland/deno/issues/19330

Shows about 1% improvement in the HTTP benchmark.
This commit is contained in:
Bartek Iwańczuk 2023-06-05 10:52:40 +02:00 committed by GitHub
parent adf41edda1
commit 21c2c01ebe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 19 additions and 16 deletions

View file

@ -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.");
}