File tree 1 file changed +15
-8
lines changed
1 file changed +15
-8
lines changed Original file line number Diff line number Diff line change 1
1
import { prettyByte } from "./prettyByte" ;
2
2
3
- const USE_NATIVE_TEXT_ENCODER = typeof TextEncoder !== "undefined" ;
3
+ // USE_TEXT_ENOCDER is opt-in because NodeJS v12's impl is much slower than pure-JavaScript version (i.e. _utf8Encode).
4
+ // Set `USE_TEXT_ENOCDER=true` to use TextEncoder.
5
+ const USE_TEXT_ENCODER = ( process . env . USE_TEXT_ENOCDER === 'true' && typeof TextEncoder !== "undefined" ) ;
4
6
5
- function _utf8Encode ( str : string ) : Array < number > {
7
+ function _utf8Encode ( str : string ) : ReadonlyArray < number > {
6
8
const len = str . length ;
7
9
8
10
const bytes : Array < number > = [ ] ;
@@ -46,12 +48,17 @@ function _utf8Encode(str: string): Array<number> {
46
48
return bytes ;
47
49
}
48
50
49
- function createNativeUtf8Encode ( ) {
50
- const encoder = new TextEncoder ( ) ;
51
+ function createUtf8Encode ( ) {
52
+ if ( USE_TEXT_ENCODER ) {
53
+ const encoder = new TextEncoder ( ) ;
51
54
52
- return ( str : string ) : Array < number > => {
53
- return Array . from ( encoder . encode ( str ) ) ;
54
- } ;
55
+ return ( str : string ) : ReadonlyArray < number > => {
56
+ // convert to array to avoid --downlevelIteration on the caller
57
+ return Array . from ( encoder . encode ( str ) ) ;
58
+ } ;
59
+ } else {
60
+ return _utf8Encode ;
61
+ }
55
62
}
56
63
57
- export const utf8Encode = USE_NATIVE_TEXT_ENCODER ? createNativeUtf8Encode ( ) : _utf8Encode ;
64
+ export const utf8Encode = createUtf8Encode ( ) ;
You can’t perform that action at this time.
0 commit comments