ashtuchkin / iconv-lite

Convert character encodings in pure javascript.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Provide performant iconv.encodeInto API (previously iconv.byteLength)

viktor-podzigun opened this issue · comments

Currently one can use:

iconv.encode(str, encoding).length

but it's slow because it creates intermediate and immediately discarded buffer.

Would be better to expose similar but efficient api:

iconv.byteLength(str, encoding)

I see, would it be then at least possible for now to provide additional method, similar to TextEncoder.encodeInto:

iconv.encodeInto(str, encoding, buf)

I have a use case, where I need to get byte lengths for bunch of strings in specific encoding.

This one is easier but still a significant change. I'll look into supporting it in the next version.

To help me prioritize and design it, could you provide more data about your use case? I.e. what's the average string length, is there anything special in the distribution of their lengths, the codec you're planning to use (at least single charcter vs double character), how many conversions per second do you have per node process? Maybe also high-level overview of what you're trying to do (if possible of course).

This additional method would help in some use cases and not in others, so I just want to make sure it'll actually make a difference in your case.

That would be great, thank you @ashtuchkin for your quick reply and support!

I would need it for the file viewer that I'm developing as part of FAR.js app. To show file content in the wrap mode I need to translate back strings that were read from file to their lengths in bytes to be able to do proper scrolling.
The encoding could be any supported one. I think its pretty common use-case.

Until now I was using Buffer encodings, but plan to switch to your nice iconv-lite lib to support more encodings.

But it's not urgent, I can use iconv.encode(str, encoding).length for now. (Even though scrolling with the mouse maybe a bit slow.)

Thanks again!

That makes sense, thank you for the context!