Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf559560

Browse files
authored
Merge pull request#3 from jsonjoy-com/perf-writer
Perf writer
2 parents1b855a4 +1e19a37 commitf559560

File tree

3 files changed

+1957
-0
lines changed

3 files changed

+1957
-0
lines changed

‎src/__bench__/bench.encodeUtf8.ts‎

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
// yarn build && npx ts-node src/__bench__/bench.encodeUtf8.ts
2+
3+
import{runBenchmark}from'./runBenchmark';
4+
import{Writer}from'../Writer';
5+
6+
consthasBuffer=typeofBuffer==='function';
7+
8+
constcompareBuffers=(a:Uint8Array,b:Uint8Array):boolean=>{
9+
if(a.length!==b.length)returnfalse;
10+
for(leti=0;i<a.length;i++){
11+
if(a[i]!==b[i])returnfalse;
12+
}
13+
returntrue;
14+
};
15+
16+
constbenchmark={
17+
name:'encodeUtf8',
18+
warmup:1000,
19+
payloads:[
20+
{
21+
name:(data:any)=>`Single character,${newTextEncoder().encode(data).length} bytes`,
22+
data:'a',
23+
},
24+
{
25+
name:(data:any)=>`"Hello",${newTextEncoder().encode(data).length} bytes`,
26+
data:'Hello',
27+
},
28+
{
29+
name:(data:any)=>`Short text with emoji,${newTextEncoder().encode(data).length} bytes`,
30+
data:'Hi, Mike 👋!',
31+
},
32+
{
33+
name:(data:any)=>`Repeating ASCII characters,${newTextEncoder().encode(data).length} bytes`,
34+
data:'a'.repeat(2),
35+
},
36+
{
37+
name:(data:any)=>`Repeating ASCII characters,${newTextEncoder().encode(data).length} bytes`,
38+
data:'a'.repeat(4),
39+
},
40+
{
41+
name:(data:any)=>`Repeating ASCII characters,${newTextEncoder().encode(data).length} bytes`,
42+
data:'a'.repeat(8),
43+
},
44+
{
45+
name:(data:any)=>`Repeating ASCII characters,${newTextEncoder().encode(data).length} bytes`,
46+
data:'abcd'.repeat(3),
47+
},
48+
{
49+
name:(data:any)=>`Repeating ASCII characters,${newTextEncoder().encode(data).length} bytes`,
50+
data:'abcd'.repeat(4),
51+
},
52+
{
53+
name:(data:any)=>`Repeating ASCII characters,${newTextEncoder().encode(data).length} bytes`,
54+
data:'abcd'.repeat(8),
55+
},
56+
{
57+
name:(data:any)=>`Repeating ASCII characters,${newTextEncoder().encode(data).length} bytes`,
58+
data:'abcd'.repeat(16),
59+
},
60+
{
61+
name:(data:any)=>`Repeating ASCII characters,${newTextEncoder().encode(data).length} bytes`,
62+
data:'abcd'.repeat(32),
63+
},
64+
{
65+
name:(data:any)=>`Repeating ASCII characters,${newTextEncoder().encode(data).length} bytes`,
66+
data:'abcd'.repeat(64),
67+
},
68+
{
69+
name:(data:any)=>`Repeating ASCII characters,${newTextEncoder().encode(data).length} bytes`,
70+
data:'abcd'.repeat(128),
71+
},
72+
{
73+
name:(data:any)=>`Multibyte characters (Latin-1),${newTextEncoder().encode(data).length} bytes`,
74+
data:'ä'.repeat(64),
75+
},
76+
{
77+
name:(data:any)=>`Multibyte characters (Latin-1),${newTextEncoder().encode(data).length} bytes`,
78+
data:'ä'.repeat(128),
79+
},
80+
{
81+
name:(data:any)=>`CJK characters,${newTextEncoder().encode(data).length} bytes`,
82+
data:'中'.repeat(32),
83+
},
84+
{
85+
name:(data:any)=>`CJK characters,${newTextEncoder().encode(data).length} bytes`,
86+
data:'中'.repeat(64),
87+
},
88+
{
89+
name:(data:any)=>`Emoji,${newTextEncoder().encode(data).length} bytes`,
90+
data:'😀'.repeat(16),
91+
},
92+
{
93+
name:(data:any)=>`Emoji,${newTextEncoder().encode(data).length} bytes`,
94+
data:'😀'.repeat(32),
95+
},
96+
{
97+
name:(data:any)=>`Mixed scripts,${newTextEncoder().encode(data).length} bytes`,
98+
data:'Hello мир 中国 🌍'.repeat(8),
99+
},
100+
{
101+
name:(data:any)=>`Mixed scripts,${newTextEncoder().encode(data).length} bytes`,
102+
data:'Hello мир 中国 🌍'.repeat(16),
103+
},
104+
],
105+
test:(data:any,result:any)=>{
106+
constexpected=newTextEncoder().encode(data);
107+
returncompareBuffers(result,expected);
108+
},
109+
runners:[
110+
{
111+
name:'TextEncoder',
112+
setup:()=>{
113+
constencoder=newTextEncoder();
114+
return(data:any)=>encoder.encode(data);
115+
},
116+
},
117+
...(hasBuffer
118+
?[
119+
{
120+
name:'Buffer.from()',
121+
setup:()=>(data:any)=>newUint8Array(Buffer.from(data,'utf8')),
122+
},
123+
]
124+
:[]),
125+
{
126+
name:'Writer.utf8()',
127+
setup:()=>{
128+
constwriter=newWriter();
129+
return(data:any)=>{
130+
writer.ensureCapacity(data.length*4);
131+
writer.utf8(data);
132+
returnwriter.flush();
133+
};
134+
},
135+
},
136+
{
137+
name:'Writer.utf8Native()',
138+
setup:()=>{
139+
constwriter=newWriter();
140+
return(data:any)=>{
141+
writer.ensureCapacity(data.length*4);
142+
writer.utf8Native(data);
143+
returnwriter.flush();
144+
};
145+
},
146+
},
147+
{
148+
name:'Writer.ascii() (ASCII only)',
149+
setup:()=>{
150+
constwriter=newWriter();
151+
return(data:any)=>{
152+
writer.ensureCapacity(data.length);
153+
writer.ascii(data);
154+
returnwriter.flush();
155+
};
156+
},
157+
},
158+
],
159+
};
160+
161+
runBenchmark(benchmark);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp