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

Performance improvement in unicode decode #662

Merged
merged 4 commits into from
Dec 3, 2020
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/IonUnicode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
* permissions and limitations under the License.
*/

const JS_DECODER_MAX_BYTES = 512;

// Check whether this runtime supports the `TextDecoder` feature
let textDecoder;
if (global["TextDecoder"] != null) {
textDecoder = new global["TextDecoder"]("utf8", { fatal: true });
} else {
textDecoder = null;
}

/**
* @file Constants and helper methods for Unicode.
* @see https://amzn.github.io/ion-docs/stringclob.html
Expand Down Expand Up @@ -60,6 +70,10 @@ export function encodeUtf8(s: string): Uint8Array {
}

export function decodeUtf8(bytes: Uint8Array): string {
// for bytes > 512 use TextDecoder method - decode()
if (bytes.length > JS_DECODER_MAX_BYTES && textDecoder != null) {
return textDecoder.decode(bytes);
}
let i = 0,
s = "",
c;
Expand Down