Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

napi: Improve performance creating strings #26439

Closed
wants to merge 4 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/js_native_api_v8.cc
Expand Up @@ -1320,7 +1320,7 @@ napi_status napi_create_string_latin1(napi_env env,
auto str_maybe =
v8::String::NewFromOneByte(isolate,
reinterpret_cast<const uint8_t*>(str),
v8::NewStringType::kInternalized,
v8::NewStringType::kNormal,
length);
CHECK_MAYBE_EMPTY(env, str_maybe, napi_generic_failure);

Expand All @@ -1335,10 +1335,17 @@ napi_status napi_create_string_utf8(napi_env env,
CHECK_ENV(env);
CHECK_ARG(env, result);

v8::Local<v8::String> s;
CHECK_NEW_FROM_UTF8_LEN(env, s, str, length);

*result = v8impl::JsValueFromV8LocalValue(s);
RETURN_STATUS_IF_FALSE(env,
(length == NAPI_AUTO_LENGTH) || length <= INT_MAX,
napi_invalid_arg);
anthony-tuininga marked this conversation as resolved.
Show resolved Hide resolved
auto isolate = env->isolate;
auto str_maybe =
v8::String::NewFromUtf8(isolate,
str,
v8::NewStringType::kNormal,
static_cast<int>(length));
CHECK_MAYBE_EMPTY(env, str_maybe, napi_generic_failure);
*result = v8impl::JsValueFromV8LocalValue(str_maybe.ToLocalChecked());
return napi_clear_last_error(env);
}

Expand All @@ -1353,7 +1360,7 @@ napi_status napi_create_string_utf16(napi_env env,
auto str_maybe =
v8::String::NewFromTwoByte(isolate,
reinterpret_cast<const uint16_t*>(str),
v8::NewStringType::kInternalized,
v8::NewStringType::kNormal,
length);
CHECK_MAYBE_EMPTY(env, str_maybe, napi_generic_failure);

Expand Down