Skip to content

browser: use TextDecoder and TextEncoder for buffer #1458

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

Closed
wants to merge 1 commit into from

Conversation

myfreeer
Copy link
Contributor

@myfreeer myfreeer commented Sep 12, 2020

Summary

Doing a profiling in chrome dev tools shows that the Buffer.toString() and Buffer.from(string) is using unexpected long cpu time. With the native TextDecoder and TextEncoder it can get much faster in browsers supporting it.
On browsers not supporting TextDecoder, like Internet Explorer, Buffer.toString() and Buffer.from(string) would not be changed.

References:
feross/buffer#268
feross/buffer#60
https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder
https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder

Test plan

Profiling below is based on reading spec/integration/data/huge.xlsx.

Profiling result before patch

before
Profile-before.json.gz

Profiling result after patch

after
Profile-after.json.gz

Doing a profiling in chrome dev tools shows that the `Buffer.toString()` and `Buffer.from(string)` is using unexpected long cpu time. With the native TextDecoder and TextEncoder it can get much faster in browsers supporting it.
On browsers not supporting TextDecoder, like Internet Explorer, `Buffer.toString()` and `Buffer.from(string)` would not be changed.

References:
feross/buffer#268
feross/buffer#60
https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder
https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder
@myfreeer myfreeer force-pushed the browser-patch-buffer branch from 5cd3516 to 6b46197 Compare October 1, 2020 11:53
@alubbe
Copy link
Member

alubbe commented Oct 1, 2020

This is pretty cool, but I don't like that we're monkey-patching a global dependency. Can't we just use TextDecoder and TextEncoder locally in our code base instead of Buffer.from?

myfreeer added a commit to myfreeer/exceljs that referenced this pull request Oct 3, 2020
Doing a profiling in chrome dev tools shows that the `Buffer.toString()` and `Buffer.from(string)` is using unexpected long cpu time. With the native TextDecoder and TextEncoder it can get much faster in browsers supporting it.
On browsers not supporting TextDecoder, like Internet Explorer, this would fallback to original `Buffer.toString()` and `Buffer.from(string)`.
This implements almost the same of exceljs#1458 in a non monkey-patching way covering xlsx only.
Closes exceljs#1458

References:
feross/buffer#268
feross/buffer#60
https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder
https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder
myfreeer added a commit to myfreeer/exceljs that referenced this pull request Oct 3, 2020
Doing a profiling in chrome dev tools shows that the `Buffer.toString()` and `Buffer.from(string)` is using unexpected long cpu time. With the native TextDecoder and TextEncoder it can get much faster in browsers supporting it.
On browsers not supporting TextDecoder, like Internet Explorer, this would fallback to original `Buffer.toString()` and `Buffer.from(string)`.
This implements almost the same of exceljs#1458 in a non monkey-patching way covering xlsx only.
Closes exceljs#1458

References:
feross/buffer#268
feross/buffer#60
https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder
https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder
@alubbe alubbe closed this Oct 5, 2020
myfreeer added a commit to myfreeer/exceljs that referenced this pull request Oct 5, 2020
Doing a profiling in chrome dev tools shows that the `Buffer.toString()` and `Buffer.from(string)` is using unexpected long cpu time. With the native TextDecoder and TextEncoder it can get much faster in browsers supporting it.
On browsers not supporting TextDecoder, like Internet Explorer, this would fallback to original `Buffer.toString()` and `Buffer.from(string)`.
This implements almost the same of exceljs#1458 in a non monkey-patching way covering xlsx only.
Closes exceljs#1458

References:
feross/buffer#268
feross/buffer#60
https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder
https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder
myfreeer added a commit to myfreeer/exceljs that referenced this pull request Oct 5, 2020
Doing a profiling in chrome dev tools shows that the `Buffer.toString()` and `Buffer.from(string)` is using unexpected long cpu time. With the native TextDecoder and TextEncoder it can get much faster in browsers supporting it.
On browsers not supporting TextDecoder, like Internet Explorer, this would fallback to original `Buffer.toString()` and `Buffer.from(string)`.
This implements almost the same of exceljs#1458 in a non monkey-patching way covering xlsx only.
Closes exceljs#1458

References:
feross/buffer#268
feross/buffer#60
https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder
https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants