Skip to content

Commit

Permalink
[JSC] Early return from isCanonicalNumericIndexString when it is defi…
Browse files Browse the repository at this point in the history
…nitely not a number

https://bugs.webkit.org/show_bug.cgi?id=259230
rdar://112292849

Reviewed by Alexey Shvayka.

This patch makes isCanonicalNumericIndexString early return for obvious non number cases
so that we avoid super costly number-to-string & string-to-number path.

* Source/JavaScriptCore/runtime/PropertyName.h:
(JSC::isCanonicalNumericIndexString):

Canonical link: https://commits.webkit.org/266080@main
  • Loading branch information
Constellation committed Jul 15, 2023
1 parent 8e00fd0 commit 7b6f950
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions Source/JavaScriptCore/runtime/PropertyName.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,27 @@ ALWAYS_INLINE bool isCanonicalNumericIndexString(UniquedStringImpl* propertyName
return false;
if (propertyName->isSymbol())
return false;
if (equal(propertyName, "-0"_s))
return true;

StringView view(propertyName);
unsigned length = view.length();
if (!length)
return false;
UChar first = view[0];
if (length == 1)
return isASCIIDigit(first);
UChar second = view[1];
if (first == '-') {
// -Infinity case should go to the slow path. -NaN cannot exist since it becomes NaN.
if (!isASCIIDigit(second) && (length != strlen("-Infinity") || second != 'I'))
return false;
if (length == 2) // Including -0, and it should be accepted.
return true;
} else if (!isASCIIDigit(first)) {
// Infinity and NaN should go to the slow path.
if (!(length == strlen("Infinity") || first == 'I') && !(length == strlen("NaN") || first == 'N'))
return false;
}

double index = jsToNumber(propertyName);
NumberToStringBuffer buffer;
const char* indexString = WTF::numberToString(index, buffer);
Expand Down

0 comments on commit 7b6f950

Please sign in to comment.