-
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
Buffer.toString vs TextDecoder #18
Comments
Can you run the |
Node 19.1 |
The correct comparison is this. Since import cronometro from 'cronometro'
import assert from 'node:assert'
const decoder = new TextDecoder()
const encoder = new TextEncoder()
const STR = 'a'.repeat(1024)
const ENCODED_STR = encoder.encode(STR)
const BUF = Buffer.from(STR)
cronometro({
buffer() {
const tmp = BUF.toString('utf-8')
assert(tmp.length)
},
decoder() {
const tmp = decoder.decode(ENCODED_STR)
assert(tmp.length)
},
}) Output (for large inputs)
Output (for smaller inputs)
|
Additionally, Node 19.1 does not have the following commit: nodejs/node@4ac830e. I'm running my benchmarks on the |
Should we remove this from the agenda, or do you think it's worth a talk @ronag? |
I mean for small strings we are still significantly slower? |
It seems like there is an opportunity here to also improve the performance of Buffer.toString. |
In the last performance meeting, we talked about updating documentation for preferring TextDecoder over buffer.toString for utf-8 values. If anybody is happy to open an issue to update Node documentation, that will be greatly appreciated. |
I think it would be better to make Buffer.toString as fast as TextDecoder. |
@ronag Could you post your benchmark? I copied @anonrig's script. The results posted in #18 (comment) showed way too much variance to be significant, so I increased the number of iterations. I now barely see any difference between the two tests in node 19.3.0. 1 MiB buffer: tniessen@ubuntuserver:~/dev/node/bench-46334$ node bench-46334.mjs
╔══════════════╤═════════╤════════════════╤═══════════╗
║ Slower tests │ Samples │ Result │ Tolerance ║
╟──────────────┼─────────┼────────────────┼───────────╢
║ buffer │ 10000 │ 2090.37 op/sec │ ± 0.14 % ║
╟──────────────┼─────────┼────────────────┼───────────╢
║ Fastest test │ Samples │ Result │ Tolerance ║
╟──────────────┼─────────┼────────────────┼───────────╢
║ decoder │ 10000 │ 2118.40 op/sec │ ± 0.15 % ║
╚══════════════╧═════════╧════════════════╧═══════════╝ 1 KiB buffer: tniessen@ubuntuserver:~/dev/node/bench-46334$ node bench-46334.mjs
╔══════════════╤═════════╤═══════════════════╤═══════════╗
║ Slower tests │ Samples │ Result │ Tolerance ║
╟──────────────┼─────────┼───────────────────┼───────────╢
║ buffer │ 100000 │ 3320487.36 op/sec │ ± 4.30 % ║
╟──────────────┼─────────┼───────────────────┼───────────╢
║ Fastest test │ Samples │ Result │ Tolerance ║
╟──────────────┼─────────┼───────────────────┼───────────╢
║ decoder │ 100000 │ 3490502.15 op/sec │ ± 3.77 % ║
╚══════════════╧═════════╧═══════════════════╧═══════════╝ |
I recommend closing this issue after rereading @tniessen's comment. Please open a new issue if there is a proof that we need to invest more time on this. |
The text was updated successfully, but these errors were encountered: