Skip to content

Commit

Permalink
Use TextDecoder API to read longer strings based on length heuristic
Browse files Browse the repository at this point in the history
  • Loading branch information
ahk committed Oct 11, 2019
1 parent e264548 commit 21bbaf2
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ Pbf.Fixed32 = 5; // 32-bit: float, fixed32, sfixed32
var SHIFT_LEFT_32 = (1 << 16) * (1 << 16),
SHIFT_RIGHT_32 = 1 / SHIFT_LEFT_32;

// Threshold chosen based on both benchmarking and knowledge about browser string
// data structures (which currently switch structure types at 12 bytes or more)
const TEXT_DECODER_MIN_LENGTH = 12;
const utf8TextDecoder = (typeof TextDecoder === 'undefined') ?
null : new TextDecoder('utf8');

Pbf.prototype = {

destroy: function() {
Expand Down Expand Up @@ -112,9 +118,18 @@ Pbf.prototype = {
},

readString: function() {
var end = this.readVarint() + this.pos,
const end = this.readVarint() + this.pos;

let str;
if (end - this.pos >= TEXT_DECODER_MIN_LENGTH && utf8TextDecoder) {
// longer strings are fast with the built-in browser TextDecoder API
str = readUtf8TextDecoder(this.buf, this.pos, end);
} else {
// short strings are fast with our custom implementation
str = readUtf8(this.buf, this.pos, end);
}
this.pos = end;

return str;
},

Expand Down Expand Up @@ -573,6 +588,10 @@ function readUtf8(buf, pos, end) {
return str;
}

function readUtf8TextDecoder(buf, pos, end) {
return utf8TextDecoder.decode(buf.subarray(pos, end));
}

function writeUtf8(buf, str, pos) {
for (var i = 0, c, lead; i < str.length; i++) {
c = str.charCodeAt(i); // code point
Expand Down

0 comments on commit 21bbaf2

Please sign in to comment.