FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
node/benchmark/string_decoder/string-decoder.js at main · nodejs/node · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
nodejs
/
node
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
36.6k
Star
120k
Code
Issues
632
Pull requests
746
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
node
/
benchmark
/
string_decoder
/
string-decoder.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
88 lines (78 loc) · 2.21 KB
Breadcrumbs
node
/
benchmark
/
string_decoder
/
string-decoder.js
Copy path
File metadata and controls
88 lines (78 loc) · 2.21 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
'use strict'
;
const
common
=
require
(
'../common.js'
)
;
const
StringDecoder
=
require
(
'string_decoder'
)
.
StringDecoder
;
const
assert
=
require
(
'node:assert'
)
;
const
bench
=
common
.
createBenchmark
(
main
,
{
encoding
:
[
'ascii'
,
'utf8'
,
'base64-utf8'
,
'base64-ascii'
,
'utf16le'
]
,
inLen
:
[
32
,
128
,
1024
]
,
chunkLen
:
[
16
,
256
,
1024
]
,
n
:
[
25e5
]
,
}
)
;
const
UTF8_ALPHA
=
'Blåbærsyltetøy'
;
const
ASC_ALPHA
=
'Blueberry jam'
;
const
UTF16_BUF
=
Buffer
.
from
(
'Blåbærsyltetøy'
,
'utf16le'
)
;
function
main
(
{
encoding
,
inLen
,
chunkLen
,
n
}
)
{
let
alpha
;
let
buf
;
const
chunks
=
[
]
;
let
str
=
''
;
const
isBase64
=
(
encoding
===
'base64-ascii'
||
encoding
===
'base64-utf8'
)
;
if
(
encoding
===
'ascii'
||
encoding
===
'base64-ascii'
)
alpha
=
ASC_ALPHA
;
else
if
(
encoding
===
'utf8'
||
encoding
===
'base64-utf8'
)
alpha
=
UTF8_ALPHA
;
else
if
(
encoding
===
'utf16le'
)
{
buf
=
UTF16_BUF
;
str
=
Buffer
.
alloc
(
0
)
;
}
else
throw
new
Error
(
'Bad encoding'
)
;
const
sd
=
new
StringDecoder
(
isBase64
?
'base64'
:
encoding
)
;
for
(
let
i
=
0
;
i
<
inLen
;
++
i
)
{
if
(
i
>
0
&&
(
i
%
chunkLen
)
===
0
&&
!
isBase64
)
{
if
(
alpha
)
{
chunks
.
push
(
Buffer
.
from
(
str
,
encoding
)
)
;
str
=
''
;
}
else
{
chunks
.
push
(
str
)
;
str
=
Buffer
.
alloc
(
0
)
;
}
}
if
(
alpha
)
str
+=
alpha
[
i
%
alpha
.
length
]
;
else
{
let
start
=
i
;
let
end
=
i
+
2
;
if
(
i
%
2
!==
0
)
{
++
start
;
++
end
;
}
str
=
Buffer
.
concat
(
[
str
,
buf
.
slice
(
start
%
buf
.
length
,
end
%
buf
.
length
)
,
]
)
;
}
}
if
(
!
alpha
)
{
if
(
str
.
length
>
0
)
chunks
.
push
(
str
)
;
}
else
if
(
str
.
length
>
0
&&
!
isBase64
)
chunks
.
push
(
Buffer
.
from
(
str
,
encoding
)
)
;
if
(
isBase64
)
{
str
=
Buffer
.
from
(
str
,
'utf8'
)
.
toString
(
'base64'
)
;
while
(
str
.
length
>
0
)
{
const
len
=
Math
.
min
(
chunkLen
,
str
.
length
)
;
chunks
.
push
(
Buffer
.
from
(
str
.
substring
(
0
,
len
)
,
'utf8'
)
)
;
str
=
str
.
substring
(
len
)
;
}
}
const
nChunks
=
chunks
.
length
;
let
avoidDeadCode
;
bench
.
start
(
)
;
for
(
let
i
=
0
;
i
<
n
;
++
i
)
{
avoidDeadCode
=
''
;
for
(
let
j
=
0
;
j
<
nChunks
;
++
j
)
avoidDeadCode
+=
sd
.
write
(
chunks
[
j
]
)
;
}
bench
.
end
(
n
)
;
assert
.
ok
(
avoidDeadCode
)
;
}
Back
|
FazBrowse Home
|
New Git URL