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

Reducing Buffer.from(str, ENC_1).toString(ENC_2) usage across Node #16

Closed
anonrig opened this issue Nov 17, 2022 · 4 comments
Closed

Reducing Buffer.from(str, ENC_1).toString(ENC_2) usage across Node #16

anonrig opened this issue Nov 17, 2022 · 4 comments
Labels
good first issue Good for newcomers

Comments

@anonrig
Copy link
Member

anonrig commented Nov 17, 2022

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.js - btoa - Buffer.from(input, 'latin1').toString('base64');
  • buffer.js - atob - Buffer.from(input, 'base64').toString('latin1')
  • _http_client.js - Buffer.from(options.auth).toString('base64')
  • random.js - Buffer.from(arrayBuffer).toString('hex')
@aduh95
Copy link

aduh95 commented Nov 17, 2022

  • random.js - Buffer.from(arrayBuffer).toString('hex')

We could probably have a JS-only method that does that without going through the Buffer API at all, which may or may not be faster than a C++ implementation.

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);
}
  • buffer.js - btoa - Buffer.from(input, 'latin1').toString('base64');
  • buffer.js - atob - Buffer.from(input, 'base64').toString('latin1')

Given we recommend against using those functions, I'm not sure we should worry about the perf of those at all.

@anonrig
Copy link
Member Author

anonrig commented Nov 21, 2022

@aduh95 Would you like to open a pull request for the 'hex' example you provided, or should I?

@aduh95
Copy link

aduh95 commented Nov 21, 2022 via email

nodejs-github-bot pushed a commit to nodejs/node that referenced this issue Nov 27, 2022
PR-URL: #45567
Refs: nodejs/performance#16
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
ErickWendel pushed a commit to ErickWendel/node that referenced this issue Nov 30, 2022
PR-URL: nodejs#45567
Refs: nodejs/performance#16
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
targos pushed a commit to nodejs/node that referenced this issue Dec 12, 2022
PR-URL: #45567
Refs: nodejs/performance#16
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
danielleadams pushed a commit to nodejs/node that referenced this issue Dec 30, 2022
PR-URL: #45567
Refs: nodejs/performance#16
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
danielleadams pushed a commit to nodejs/node that referenced this issue Dec 30, 2022
PR-URL: #45567
Refs: nodejs/performance#16
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
danielleadams pushed a commit to nodejs/node that referenced this issue Jan 3, 2023
PR-URL: #45567
Refs: nodejs/performance#16
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
danielleadams pushed a commit to nodejs/node that referenced this issue Jan 4, 2023
PR-URL: #45567
Refs: nodejs/performance#16
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
danielleadams pushed a commit to nodejs/node that referenced this issue Jan 5, 2023
PR-URL: #45567
Refs: nodejs/performance#16
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
@anonrig
Copy link
Member Author

anonrig commented Jan 16, 2023

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.

@anonrig anonrig closed this as completed Jan 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants