Skip to content

Commit 8349faa

Browse files
committed
util: improve text-decoder performance
1 parent 79e26b5 commit 8349faa

File tree

2 files changed

+8
-17
lines changed

2 files changed

+8
-17
lines changed

lib/internal/encoding.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -411,15 +411,7 @@ function makeTextDecoderICU() {
411411

412412
decode(input = empty, options = kEmptyObject) {
413413
validateDecoder(this);
414-
if (isAnyArrayBuffer(input)) {
415-
try {
416-
input = lazyBuffer().from(input);
417-
} catch {
418-
// If the buffer is detached,
419-
// use an empty Uint8Array to avoid TypeError
420-
input = empty;
421-
}
422-
} else if (!isArrayBufferView(input)) {
414+
if (!isAnyArrayBuffer(input) && !isArrayBufferView(input)) {
423415
throw new ERR_INVALID_ARG_TYPE('input',
424416
['ArrayBuffer', 'ArrayBufferView'],
425417
input);

src/node_i18n.cc

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#if defined(NODE_HAVE_I18N_SUPPORT)
4747

4848
#include "base_object-inl.h"
49+
#include "crypto/crypto_util.h"
4950
#include "node.h"
5051
#include "node_buffer.h"
5152
#include "node_errors.h"
@@ -440,7 +441,7 @@ void ConverterObject::Decode(const FunctionCallbackInfo<Value>& args) {
440441

441442
ConverterObject* converter;
442443
ASSIGN_OR_RETURN_UNWRAP(&converter, args[0].As<Object>());
443-
ArrayBufferViewContents<char> input(args[1]);
444+
crypto::ArrayBufferOrViewContents<char> input(args[1]);
444445
int flags = args[2]->Uint32Value(env->context()).ToChecked();
445446

446447
UErrorCode status = U_ZERO_ERROR;
@@ -453,12 +454,10 @@ void ConverterObject::Decode(const FunctionCallbackInfo<Value>& args) {
453454
// characters times the min char size, multiplied by 2 as unicode may
454455
// take up to 2 UChars to encode a character
455456
size_t limit = 2 * converter->min_char_size() *
456-
(!flush ?
457-
input.length() :
458-
std::max(
459-
input.length(),
460-
static_cast<size_t>(
461-
ucnv_toUCountPending(converter->conv(), &status))));
457+
(!flush ? input.size()
458+
: std::max(input.size(),
459+
static_cast<size_t>(ucnv_toUCountPending(
460+
converter->conv(), &status))));
462461
status = U_ZERO_ERROR;
463462

464463
if (limit > 0)
@@ -473,7 +472,7 @@ void ConverterObject::Decode(const FunctionCallbackInfo<Value>& args) {
473472
});
474473

475474
const char* source = input.data();
476-
size_t source_length = input.length();
475+
size_t source_length = input.size();
477476

478477
UChar* target = *result;
479478
ucnv_toUnicode(converter->conv(),

0 commit comments

Comments
 (0)