Skip to content

Commit

Permalink
js: performance improvement for TextDecoder pollyfill
Browse files Browse the repository at this point in the history
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
samuelbrian authored and saghul committed Nov 4, 2022
1 parent 9556fad commit ef2f563
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
52 changes: 52 additions & 0 deletions patches/text-encoding-utf-8+1.0.2.patch
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;
11 changes: 6 additions & 5 deletions src/js/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -8099,6 +8099,7 @@ var require_encoding_lib = __commonJS({
var end_of_stream = -1;
function Stream(tokens) {
this.tokens = [].slice.call(tokens);
this.tokens.reverse();
}
Stream.prototype = {
endOfStream: function() {
Expand All @@ -8107,24 +8108,24 @@ var require_encoding_lib = __commonJS({
read: function() {
if (!this.tokens.length)
return end_of_stream;
return this.tokens.shift();
return this.tokens.pop();
},
prepend: function(token) {
if (Array.isArray(token)) {
var tokens = token;
while (tokens.length)
this.tokens.unshift(tokens.pop());
this.tokens.push(tokens.pop());
} else {
this.tokens.unshift(token);
this.tokens.push(token);
}
},
push: function(token) {
if (Array.isArray(token)) {
var tokens = token;
while (tokens.length)
this.tokens.push(tokens.shift());
this.tokens.unshift(tokens.shift());
} else {
this.tokens.push(token);
this.tokens.unshift(token);
}
}
};
Expand Down

0 comments on commit ef2f563

Please sign in to comment.