-
Notifications
You must be signed in to change notification settings - Fork 7
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
Reducing Buffer.from(str, ENC_1).toString(ENC_2) usage across Node #16
Comments
We could probably have a JS-only method that does that without going through the function numberToHexCharCode(number) {
return (number < 10 ? 49 : 97) + number;
}
/**
* @param {ArrayBuffer} buf {}
*/
function arrayBufferToHexString(buf) {
const length = buf.byteLength;
const chars = Array(length * 2);
const view = new DataView(buf);
for (let i = 0; i < length; i++) {
const val = view.getUint8(i);
chars[2 * i] = numberToHexCharCode(val >> 4);
chars[2 * i + 1] = numberToHexCharCode(val & 0xf);
}
return Reflect.apply(String.fromCharCode, null, chars);
}
Given we recommend against using those functions, I'm not sure we should worry about the perf of those at all. |
@aduh95 Would you like to open a pull request for the 'hex' example you provided, or should I? |
I haven’t run any benchmark to validate if it’s actually faster – but I guess I could open the PR anyway and run the benchmark on Jenkins.
|
PR-URL: #45567 Refs: nodejs/performance#16 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com>
PR-URL: nodejs#45567 Refs: nodejs/performance#16 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com>
PR-URL: #45567 Refs: nodejs/performance#16 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com>
PR-URL: #45567 Refs: nodejs/performance#16 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com>
PR-URL: #45567 Refs: nodejs/performance#16 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com>
PR-URL: #45567 Refs: nodejs/performance#16 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com>
PR-URL: #45567 Refs: nodejs/performance#16 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com>
PR-URL: #45567 Refs: nodejs/performance#16 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com>
I've looked into a couple of places, but this issue is not applicable anymore. I'm closing it. In case there are any objections, please re-open it. Thanks. |
There are lots of
Buffer.from(value, ENC).toString(ENC_2)
code duplication in Node. I recommend creating a C++ function to do the same execution with less JS/C++ calls.bufferToString(value, encoding_1, encoding_2);
that returns a string.For example:
Buffer.from(input, 'latin1').toString('base64');
Buffer.from(input, 'base64').toString('latin1')
Buffer.from(options.auth).toString('base64')
Buffer.from(arrayBuffer).toString('hex')
The text was updated successfully, but these errors were encountered: