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

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

Open
viktor-podzigun opened this issue Feb 27, 2023 · 5 comments
Labels

Comments

@viktor-podzigun
Copy link

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)
@ashtuchkin
Copy link
Owner

ashtuchkin commented Feb 27, 2023 via email

@viktor-podzigun
Copy link
Author

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.

@ashtuchkin
Copy link
Owner

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.

@ashtuchkin ashtuchkin changed the title Provide performant iconv.byteLength api Provide performant iconv.encodeInto API (previously iconv.byteLength) Feb 27, 2023
@viktor-podzigun
Copy link
Author

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!

@ashtuchkin
Copy link
Owner

That makes sense, thank you for the context!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants