src: improve StringBytes::Encode perf on ASCII

PR-URL: https://github.com/nodejs/node/pull/61119
Reviewed-By: Daniel Lemire <daniel@lemire.me>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
Reviewed-By: Xuguang Mei <meixuguang@gmail.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
Nikita Skovoroda authored and Antoine du Hamel committed 2026-01-19 19:19:29 +01:00
1 parent d56066ce8c
commit b6d3caeda8
2 files changed
+25 -7

No files matched your search

+17 -7
View File
@@ -163,7 +163,24 @@ void BindingData::DecodeUTF8(const FunctionCallbackInfo<Value>& args) {
const char* data = buffer.data();
size_t length = buffer.length();
if (!ignore_bom && length >= 3) {
if (memcmp(data, "\xEF\xBB\xBF", 3) == 0) {
data += 3;
length -= 3;
}
}
if (has_fatal) {
// Are we perhaps ASCII? Then we won't have to check for UTF-8
if (!simdutf::validate_ascii_with_errors(data, length).error) {
Local<Value> ret;
if (StringBytes::Encode(env->isolate(), data, length, LATIN1)
.ToLocal(&ret)) {
args.GetReturnValue().Set(ret);
}
return;
}
auto result = simdutf::validate_utf8_with_errors(data, length);
if (result.error) {
@@ -172,13 +189,6 @@ void BindingData::DecodeUTF8(const FunctionCallbackInfo<Value>& args) {
}
}
if (!ignore_bom && length >= 3) {
if (memcmp(data, "\xEF\xBB\xBF", 3) == 0) {
data += 3;
length -= 3;
}
}
if (length == 0) return args.GetReturnValue().SetEmptyString();
Local<Value> ret;
+8
View File
@@ -531,6 +531,14 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate,
case UTF8: {
buflen = keep_buflen_in_range(buflen);
// ASCII fast path
// TODO(chalker): remove when String::NewFromUtf8 is fast enough itself
// This is cheap compared to the benefits though
if (!simdutf::validate_ascii_with_errors(buf, buflen).error) {
return ExternOneByteString::NewFromCopy(isolate, buf, buflen);
}
val =
String::NewFromUtf8(isolate, buf, v8::NewStringType::kNormal, buflen);
Local<String> str;