-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
js: performance improvement for TextDecoder pollyfill
The UTF-8-only encoder/decoder was performing poorly due to inefficient array manipulation. Applied changes from the original text-encoding package that reduce the amount of copying. Relates to #316
- Loading branch information
1 parent
9556fad
commit ef2f563
Showing
2 changed files
with
58 additions
and
5 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,52 @@ | ||
diff --git a/node_modules/text-encoding-utf-8/lib/encoding.lib.js b/node_modules/text-encoding-utf-8/lib/encoding.lib.js | ||
index a067c51..05a1954 100644 | ||
--- a/node_modules/text-encoding-utf-8/lib/encoding.lib.js | ||
+++ b/node_modules/text-encoding-utf-8/lib/encoding.lib.js | ||
@@ -155,6 +155,7 @@ function codePointsToString(code_points) { | ||
function Stream(tokens) { | ||
/** @type {!Array.<number>} */ | ||
this.tokens = [].slice.call(tokens); | ||
+ this.tokens.reverse(); | ||
} | ||
|
||
Stream.prototype = { | ||
@@ -176,7 +177,7 @@ Stream.prototype = { | ||
read: function() { | ||
if (!this.tokens.length) | ||
return end_of_stream; | ||
- return this.tokens.shift(); | ||
+ return this.tokens.pop(); | ||
}, | ||
|
||
/** | ||
@@ -190,9 +191,9 @@ Stream.prototype = { | ||
if (Array.isArray(token)) { | ||
var tokens = /**@type {!Array.<number>}*/(token); | ||
while (tokens.length) | ||
- this.tokens.unshift(tokens.pop()); | ||
+ this.tokens.push(tokens.pop()); | ||
} else { | ||
- this.tokens.unshift(token); | ||
+ this.tokens.push(token); | ||
} | ||
}, | ||
|
||
@@ -207,9 +208,9 @@ Stream.prototype = { | ||
if (Array.isArray(token)) { | ||
var tokens = /**@type {!Array.<number>}*/(token); | ||
while (tokens.length) | ||
- this.tokens.push(tokens.shift()); | ||
+ this.tokens.unshift(tokens.shift()); | ||
} else { | ||
- this.tokens.push(token); | ||
+ this.tokens.unshift(token); | ||
} | ||
} | ||
}; | ||
@@ -639,4 +640,4 @@ function UTF8Encoder(options) { | ||
} | ||
|
||
exports.TextEncoder = TextEncoder; | ||
-exports.TextDecoder = TextDecoder; | ||
\ No newline at end of file | ||
+exports.TextDecoder = TextDecoder; |
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