@cloudpss/ubjson 0.3.0-alpha.28 → 0.3.0-alpha.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/benchmark-string.js +12 -39
- package/dist/string-decoder.d.ts +2 -0
- package/dist/string-decoder.js +3 -3
- package/dist/string-decoder.js.map +1 -1
- package/package.json +2 -2
- package/src/string-decoder.ts +3 -3
package/benchmark-string.js
CHANGED
|
@@ -1,17 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const decodeAscii = (() => {
|
|
4
|
-
try {
|
|
5
|
-
const fastDecodeAscii = String.fromCharCode.apply.bind(String.fromCharCode, String);
|
|
6
|
-
|
|
7
|
-
// 检查能否直接向 `String.fromCharCode.apply` 传入 `Uint8Array`
|
|
8
|
-
if ('\0\x01\x7f' === fastDecodeAscii(Uint8Array.from([0, 1, 0x7f]))) return fastDecodeAscii;
|
|
9
|
-
/* c8 ignore next 1 */
|
|
10
|
-
} catch {
|
|
11
|
-
/* c8 ignore next 2 */
|
|
12
|
-
}
|
|
13
|
-
return (array) => String.fromCharCode(...array);
|
|
14
|
-
})();
|
|
1
|
+
import { textDecoder, decodeAscii } from './dist/string-decoder.js';
|
|
15
2
|
|
|
16
3
|
function decode(data) {
|
|
17
4
|
if (data.every((b) => b < 128)) {
|
|
@@ -22,32 +9,19 @@ function decode(data) {
|
|
|
22
9
|
return textDecoder.decode(data);
|
|
23
10
|
}
|
|
24
11
|
|
|
25
|
-
const LOOP =
|
|
12
|
+
const LOOP = 500000;
|
|
26
13
|
|
|
27
14
|
const t = (time) => time.toFixed(10) + 'ms';
|
|
28
15
|
|
|
16
|
+
function createTestData(size) {
|
|
17
|
+
return new Uint8Array(size).fill(50);
|
|
18
|
+
}
|
|
19
|
+
|
|
29
20
|
/* eslint-disable no-console */
|
|
30
21
|
(async function () {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
new Uint8Array(20).fill(50),
|
|
34
|
-
new Uint8Array(30).fill(50),
|
|
35
|
-
new Uint8Array(40).fill(50),
|
|
36
|
-
new Uint8Array(50).fill(50),
|
|
37
|
-
new Uint8Array(60).fill(50),
|
|
38
|
-
new Uint8Array(70).fill(50),
|
|
39
|
-
new Uint8Array(80).fill(50),
|
|
40
|
-
new Uint8Array(90).fill(50),
|
|
41
|
-
new Uint8Array(100).fill(50),
|
|
42
|
-
new Uint8Array(110).fill(50),
|
|
43
|
-
new Uint8Array(120).fill(50),
|
|
44
|
-
new Uint8Array(130).fill(50),
|
|
45
|
-
new Uint8Array(140).fill(50),
|
|
46
|
-
new Uint8Array(150).fill(50),
|
|
47
|
-
new Uint8Array(200).fill(50),
|
|
48
|
-
];
|
|
22
|
+
for (let index = 50; index < 100; index++) {
|
|
23
|
+
const data = createTestData(index);
|
|
49
24
|
|
|
50
|
-
for (const data of testData) {
|
|
51
25
|
let decodeTime1 = 0;
|
|
52
26
|
let decodeTime2 = 0;
|
|
53
27
|
for (let i = 0; i < LOOP; i++) {
|
|
@@ -59,12 +33,11 @@ const t = (time) => time.toFixed(10) + 'ms';
|
|
|
59
33
|
decode(data);
|
|
60
34
|
decodeTime2 += performance.now() - start;
|
|
61
35
|
}
|
|
62
|
-
|
|
36
|
+
const ratio = decodeTime2 / decodeTime1;
|
|
63
37
|
console.log(
|
|
64
|
-
`Data: ${data.byteLength} \t${t(decodeTime1 / LOOP)} ${t(decodeTime2 / LOOP)} ${(
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
).toFixed(3)}%`,
|
|
38
|
+
`Data: ${data.byteLength} \t${t(decodeTime1 / LOOP)} ${t(decodeTime2 / LOOP)} ${(ratio * 100).toFixed(
|
|
39
|
+
3,
|
|
40
|
+
)}%`,
|
|
68
41
|
);
|
|
69
42
|
}
|
|
70
43
|
})();
|
package/dist/string-decoder.d.ts
CHANGED
package/dist/string-decoder.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const textDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: false });
|
|
2
|
-
const decodeAscii = (() => {
|
|
1
|
+
export const textDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: false });
|
|
2
|
+
export const decodeAscii = (() => {
|
|
3
3
|
try {
|
|
4
4
|
const fastDecodeAscii = String.fromCharCode.apply.bind(String.fromCharCode, String);
|
|
5
5
|
// 检查能否直接向 `String.fromCharCode.apply` 传入 `Uint8Array`
|
|
@@ -14,7 +14,7 @@ const decodeAscii = (() => {
|
|
|
14
14
|
})();
|
|
15
15
|
/** 解码 */
|
|
16
16
|
function decode(data) {
|
|
17
|
-
if (data.byteLength <
|
|
17
|
+
if (data.byteLength < 50 && // 只有小字符串有优化价值,见 benchmark-string.js
|
|
18
18
|
data.every((b) => b < 128)) {
|
|
19
19
|
// 为 ASCII 字符串优化
|
|
20
20
|
return decodeAscii(data);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string-decoder.js","sourceRoot":"","sources":["../src/string-decoder.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,GAAG,IAAI,WAAW,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"string-decoder.js","sourceRoot":"","sources":["../src/string-decoder.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,WAAW,GAAG,IAAI,WAAW,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;AAEvF,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,GAAG,EAAE;IAC7B,IAAI;QACA,MAAM,eAAe,GAAkC,MAAM,CAAC,YAAY,CAAC,KAAK,CAAC,IAAI,CACjF,MAAM,CAAC,YAAY,EACnB,MAAM,CAC8B,CAAC;QAEzC,sDAAsD;QACtD,IAAI,YAAY,KAAK,eAAe,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,EA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,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;QACjC,WAAW;QACX,IAAI,GAAG,GAAG,CAAC,CAAC;QACZ,KAAK,MAAM,IAAI,IAAI,IAAI,EAAE;YACrB,GAAG,IAAI,GAAG,CAAC;YACX,GAAG,IAAI,IAAI,CAAC;SACf;QACD,MAAM,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAC7B,IAAI,KAAK,IAAI,IAAI;YAAE,OAAO,KAAK,CAAC;QAChC,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;QAC5B,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;QACvB,OAAO,MAAM,CAAC;IAClB,CAAC;CACJ"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudpss/ubjson",
|
|
3
|
-
"version": "0.3.0-alpha.
|
|
3
|
+
"version": "0.3.0-alpha.29",
|
|
4
4
|
"author": "CloudPSS",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"keywords": [
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"prepublishOnly": "yarn build",
|
|
24
24
|
"clean": "rimraf dist",
|
|
25
25
|
"benchmark": "node ./benchmark",
|
|
26
|
-
"test": "c8 tape-es \"./test
|
|
26
|
+
"test": "c8 tape-es \"./test/**/*.js\""
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"c8": "^7.10.0",
|
package/src/string-decoder.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
const textDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: false });
|
|
1
|
+
export const textDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: false });
|
|
2
2
|
|
|
3
|
-
const decodeAscii = (() => {
|
|
3
|
+
export const decodeAscii = (() => {
|
|
4
4
|
try {
|
|
5
5
|
const fastDecodeAscii: (array: Uint8Array) => string = String.fromCharCode.apply.bind(
|
|
6
6
|
String.fromCharCode,
|
|
@@ -19,7 +19,7 @@ const decodeAscii = (() => {
|
|
|
19
19
|
/** 解码 */
|
|
20
20
|
function decode(data: Uint8Array): string {
|
|
21
21
|
if (
|
|
22
|
-
data.byteLength <
|
|
22
|
+
data.byteLength < 50 && // 只有小字符串有优化价值,见 benchmark-string.js
|
|
23
23
|
data.every((b) => b < 128)
|
|
24
24
|
) {
|
|
25
25
|
// 为 ASCII 字符串优化
|