1
1
import { suite } from 'perf-insight' ;
2
2
3
- import { decodeUtf8ByteStream , decodeUtf8N_BE , decodeUtf8N_LE , encodeUtf8N_BE , encodeUtf8N_LE } from './Utf8.js' ;
3
+ import {
4
+ decodeUtf8ByteStream ,
5
+ decodeUtf8N_BE ,
6
+ decodeUtf8N_LE ,
7
+ encodeCodePointsToUtf8Into ,
8
+ encodeTextToUtf8 ,
9
+ encodeTextToUtf8Into ,
10
+ encodeUtf8N_BE ,
11
+ encodeUtf8N_LE ,
12
+ textToCodePoints ,
13
+ } from './Utf8.js' ;
4
14
5
15
suite ( 'Utf8 encode/decode' , async ( test ) => {
6
16
const iterations = 1000 ;
7
17
const text = sampleText ( ) ;
18
+ const words = text . split ( / \s + / ) . filter ( ( a ) => ! ! a ) ;
19
+ const wordsCP = words . map ( ( word ) => [ ...word ] . map ( ( char ) => char . codePointAt ( 0 ) || 0 ) ) ;
8
20
const chars = [ ...text ] ;
21
+ const codePoints = chars . map ( ( char ) => char . codePointAt ( 0 ) || 0 ) ;
9
22
const encoder = new TextEncoder ( ) ;
10
23
const decoder = new TextDecoder ( ) ;
11
24
const scratchBuffer = new Uint8Array ( 1024 ) ;
@@ -29,6 +42,16 @@ suite('Utf8 encode/decode', async (test) => {
29
42
}
30
43
} ) ;
31
44
45
+ test ( 'TextEncoder.encodeInto by char' , ( ) => {
46
+ const buffer = new Uint8Array ( scratchBuffer . buffer , 0 , 4 ) ;
47
+ for ( let i = iterations ; i > 0 ; -- i ) {
48
+ for ( const char of chars ) {
49
+ buffer [ 0 ] = 0 ;
50
+ encoder . encodeInto ( char , buffer ) ;
51
+ }
52
+ }
53
+ } ) ;
54
+
32
55
test ( 'encodeUtf8N_BE' , ( ) => {
33
56
for ( let i = iterations ; i > 0 ; -- i ) {
34
57
for ( const char of chars ) {
@@ -50,6 +73,146 @@ suite('Utf8 encode/decode', async (test) => {
50
73
}
51
74
}
52
75
} ) ;
76
+
77
+ test ( 'TextEncoder.encodeInto text' , ( ) => {
78
+ const buffer = scratchBuffer ;
79
+ const _text = text ;
80
+ for ( let i = iterations ; i > 0 ; -- i ) {
81
+ encoder . encodeInto ( _text , buffer ) ;
82
+ }
83
+ } ) ;
84
+
85
+ test ( 'Buffer.write text' , ( ) => {
86
+ const buffer = Buffer . from ( scratchBuffer . buffer ) ;
87
+ // const _text = text;
88
+ for ( let i = iterations ; i > 0 ; -- i ) {
89
+ buffer . write ( text , 'utf16le' ) ;
90
+ }
91
+ } ) ;
92
+
93
+ test ( 'encodeCodePointsInto' , ( ) => {
94
+ const buffer = scratchBuffer ;
95
+ const points = codePoints ;
96
+ for ( let i = iterations ; i > 0 ; -- i ) {
97
+ encodeCodePointsToUtf8Into ( points , buffer ) ;
98
+ }
99
+ } ) ;
100
+
101
+ test ( `TextEncoder.encodeInto words (${ words . length } )` , ( ) => {
102
+ const buffer = scratchBuffer ;
103
+ const _words = words ;
104
+ for ( let i = iterations ; i > 0 ; -- i ) {
105
+ for ( const word of _words ) {
106
+ encoder . encodeInto ( word , buffer ) ;
107
+ }
108
+ }
109
+ } ) ;
110
+
111
+ test ( `encodeCodePointsInto wordsCP (${ words . length } )` , ( ) => {
112
+ const buffer = scratchBuffer ;
113
+ const words = wordsCP ;
114
+ for ( let i = iterations ; i > 0 ; -- i ) {
115
+ for ( const points of words ) {
116
+ encodeCodePointsToUtf8Into ( points , buffer ) ;
117
+ }
118
+ }
119
+ } ) ;
120
+
121
+ test ( `encodeCodePointsInto Array wordsCP (${ words . length } )` , ( ) => {
122
+ const buffer = new Array ( 100 ) ;
123
+ const words = wordsCP ;
124
+ for ( let i = iterations ; i > 0 ; -- i ) {
125
+ for ( const points of words ) {
126
+ encodeCodePointsToUtf8Into ( points , buffer ) ;
127
+ }
128
+ }
129
+ } ) ;
130
+
131
+ test ( `encodeCodePointsInto wordsCP .codePointAt (${ words . length } )` , ( ) => {
132
+ const buffer = scratchBuffer ;
133
+ const _words = words ;
134
+ for ( let i = iterations ; i > 0 ; -- i ) {
135
+ for ( const word of _words ) {
136
+ encodeCodePointsToUtf8Into (
137
+ [ ...word ] . map ( ( a ) => a . codePointAt ( 0 ) || 0 ) ,
138
+ buffer ,
139
+ ) ;
140
+ }
141
+ }
142
+ } ) ;
143
+
144
+ test ( `encodeTextToUtf8Into Uint8Array words (${ words . length } )` , ( ) => {
145
+ const buffer = scratchBuffer ;
146
+ const _words = words ;
147
+ for ( let i = iterations ; i > 0 ; -- i ) {
148
+ for ( const word of _words ) {
149
+ encodeTextToUtf8Into ( word , buffer ) ;
150
+ }
151
+ }
152
+ } ) ;
153
+
154
+ test ( `encodeTextToUtf8Into array words (${ words . length } )` , ( ) => {
155
+ const buffer = new Array ( 100 ) ;
156
+ const _words = words ;
157
+ for ( let i = iterations ; i > 0 ; -- i ) {
158
+ for ( const word of _words ) {
159
+ encodeTextToUtf8Into ( word , buffer ) ;
160
+ }
161
+ }
162
+ } ) ;
163
+
164
+ test ( `encoder.encode(word) to array words (${ words . length } )` , ( ) => {
165
+ const _words = words ;
166
+ for ( let i = iterations ; i > 0 ; -- i ) {
167
+ for ( const word of _words ) {
168
+ [ ...encoder . encode ( word ) ] ;
169
+ }
170
+ }
171
+ } ) ;
172
+
173
+ test ( `encodeTextToUtf8 array words (${ words . length } )` , ( ) => {
174
+ const _words = words ;
175
+ for ( let i = iterations ; i > 0 ; -- i ) {
176
+ for ( const word of _words ) {
177
+ encodeTextToUtf8 ( word ) ;
178
+ }
179
+ }
180
+ } ) ;
181
+
182
+ const charToUtf8Map = new Map < string , number [ ] > (
183
+ [ ...new Set ( [ ...sampleText ( ) ] ) ] . map ( ( char ) => [ char , encodeTextToUtf8 ( char ) ] as const ) ,
184
+ ) ;
185
+
186
+ test ( `encodeTextToUtf8 to array with lookup (${ words . length } )` , ( ) => {
187
+ const _words = words ;
188
+ for ( let i = iterations ; i > 0 ; -- i ) {
189
+ for ( const word of _words ) {
190
+ const a : number [ ] = new Array ( word . length * 2 ) ;
191
+ let i = 0 ;
192
+ for ( const c of word ) {
193
+ const u8 = charToUtf8Map . get ( c ) ;
194
+ for ( const u of u8 || [ ] ) {
195
+ a [ i ++ ] = u ;
196
+ }
197
+ }
198
+ a . length = i ;
199
+ }
200
+ }
201
+ } ) ;
202
+
203
+ test ( 'textToCodePoints' , ( ) => {
204
+ const _text = text ;
205
+ for ( let i = iterations ; i > 0 ; -- i ) {
206
+ textToCodePoints ( _text ) ;
207
+ }
208
+ } ) ;
209
+
210
+ test ( 'textToCodePoints map' , ( ) => {
211
+ const _text = text ;
212
+ for ( let i = iterations ; i > 0 ; -- i ) {
213
+ [ ..._text ] . map ( ( a ) => a . codePointAt ( 0 ) || 0 ) ;
214
+ }
215
+ } ) ;
53
216
} ) ;
54
217
55
218
suite ( 'Utf8 decode buffer' , async ( test ) => {
0 commit comments