Skip to content
Permalink
Browse files
String.prototype.charCodeAt() should use StringView.
<https://webkit.org/b/145353>

Reviewed by Darin Adler.

Use JSString::view() in charCodeAt() to avoid reifying the JSString if it's
a substring. This avoids StringImpl allocation in some cases and ref churn
in all cases.

* runtime/StringPrototype.cpp:
(JSC::stringProtoFuncCharCodeAt):


Canonical link: https://commits.webkit.org/163382@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@184866 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
Andreas Kling committed May 26, 2015
1 parent 81e1afd commit d83a4954c5672955d2097a6edf444513d0a663c5
Showing 2 changed files with 19 additions and 9 deletions.
@@ -1,3 +1,17 @@
2015-05-26 Andreas Kling <akling@apple.com>

String.prototype.charCodeAt() should use StringView.
<https://webkit.org/b/145353>

Reviewed by Darin Adler.

Use JSString::view() in charCodeAt() to avoid reifying the JSString if it's
a substring. This avoids StringImpl allocation in some cases and ref churn
in all cases.

* runtime/StringPrototype.cpp:
(JSC::stringProtoFuncCharCodeAt):

2015-05-26 Andreas Kling <akling@apple.com>

String.prototype.charAt() should use StringView.
@@ -804,21 +804,17 @@ EncodedJSValue JSC_HOST_CALL stringProtoFuncCharCodeAt(ExecState* exec)
JSValue thisValue = exec->thisValue();
if (!checkObjectCoercible(thisValue))
return throwVMTypeError(exec);
String s = thisValue.toString(exec)->value(exec);
unsigned len = s.length();
StringView string = thisValue.toString(exec)->view(exec);
JSValue a0 = exec->argument(0);
if (a0.isUInt32()) {
uint32_t i = a0.asUInt32();
if (i < len) {
if (s.is8Bit())
return JSValue::encode(jsNumber(s.characters8()[i]));
return JSValue::encode(jsNumber(s.characters16()[i]));
}
if (i < string.length())
return JSValue::encode(jsNumber(string[i]));
return JSValue::encode(jsNaN());
}
double dpos = a0.toInteger(exec);
if (dpos >= 0 && dpos < len)
return JSValue::encode(jsNumber(s[static_cast<int>(dpos)]));
if (dpos >= 0 && dpos < string.length())
return JSValue::encode(jsNumber(string[static_cast<int>(dpos)]));
return JSValue::encode(jsNaN());
}

0 comments on commit d83a495

Please sign in to comment.