forked from exceljs/exceljs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
xlsx: use TextDecoder and TextEncoder in browser
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
- Loading branch information
Showing
5 changed files
with
57 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// eslint-disable-next-line node/no-unsupported-features/node-builtins | ||
const textDecoder = typeof TextDecoder === 'undefined' ? null : new TextDecoder('utf-8'); | ||
|
||
function bufferToString(chunk) { | ||
if (typeof chunk === 'string') { | ||
return chunk; | ||
} | ||
if (textDecoder) { | ||
return textDecoder.decode(chunk); | ||
} | ||
return chunk.toString(); | ||
} | ||
|
||
exports.bufferToString = bufferToString; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// eslint-disable-next-line node/no-unsupported-features/node-builtins | ||
const textEncoder = typeof TextEncoder === 'undefined' ? null : new TextEncoder('utf-8'); | ||
const {Buffer} = require('buffer'); | ||
|
||
function stringToBuffer(str) { | ||
if (typeof str !== 'string') { | ||
return str; | ||
} | ||
if (textEncoder) { | ||
return Buffer.from(textEncoder.encode(str).buffer); | ||
} | ||
return Buffer.from(str); | ||
} | ||
|
||
exports.stringToBuffer = stringToBuffer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters