src: optimize buffer transcode for matching encodings

This commit is contained in:
JonasBa 2024-08-22 14:40:28 -04:00
parent e70bd47da9
commit 9af9206b4d
No known key found for this signature in database
GPG Key ID: 2BF82B57DD62D602
1 changed files with 8 additions and 0 deletions

View File

@ -287,6 +287,14 @@ void Transcode(const FunctionCallbackInfo<Value>&args) {
const enum encoding toEncoding = ParseEncoding(isolate, args[2], BUFFER);
if (SupportedEncoding(fromEncoding) && SupportedEncoding(toEncoding)) {
// If encodings are the same then there is nothing to transcode and we can
// just copy the buffer
if (fromEncoding == toEncoding) {
MaybeStackBuffer<char> stackbuf(input.length());
memcpy(stackbuf.out(), input.data(), input.length());
args.GetReturnValue().Set(Buffer::New(env, &stackbuf).ToLocalChecked());
return;
}
TranscodeFunc tfn = &Transcode;
switch (fromEncoding) {
case ASCII: