-
-
Notifications
You must be signed in to change notification settings - Fork 19
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
toHexString util: try String.fromCharCode api #277
Comments
For NodeJS where performance is important we should just use Buffer utils which will probably be the faster and more memory efficient.
Efficient in terms of CPU time, memory or what specifically? |
in CPU time, but I guess could be better in terms of memory too since there is no created strings in the middle (need more benchmarks to see) export function toHexString2(bytes: Uint8Array): string {
const chunks = new Array<number>(bytes.length * 2 + 2);
chunks[0] = 48;
// "x".charCodeAt(0)
chunks[1] = 120;
for (let i = 0; i < bytes.length; i++) {
const byte = bytes[i];
const first = (byte & 0xf0) >> 4;
const second = byte & 0x0f;
// "0".charCodeAt(0) = 48
// "a".charCodeAt(0) = 97 => delta = 87
chunks[2 + 2 * i] = first < 10 ? first + 48 : first + 87;
chunks[2 + 2 * i + 1] = second < 10 ? second + 48 : second + 87;
}
// return String.fromCharCode.apply(String, chunks);
return String.fromCharCode(...chunks);
} some quick benchmarks:
|
@tuyennhv For memory efficiency there's this library that flattens strings. Check it out it's magic https://github.com/davidmarkclements/flatstr |
The fromCharCode approach looks good. Probably the best browser-compatible implementation |
Note this was recommended by Ben (the libuv mantainer) |
No longer a priority since this is fixed in Lodestar |
Is your feature request related to a problem? Please describe.
Refer to achingbrain/uint8arrays#30 (comment)
Given how efficient protobuf creates a string from Uint8Array in one go, we could try to use
String.fromCharCode()
api for ourtoHexString()
function without using string concatenation (which create temporary strings that causegc
run more frequently)Describe the solution you'd like
The text was updated successfully, but these errors were encountered: