@cloudpss/ubjson 0.3.0-alpha.27 → 0.3.0-alpha.28

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.
@@ -12,6 +12,16 @@ const decodeAscii = (() => {
12
12
  }
13
13
  return (array) => String.fromCharCode(...array);
14
14
  })();
15
+ /** 解码 */
16
+ function decode(data) {
17
+ if (data.byteLength < 80 && // 只有小字符串有优化价值,见 benchmark-string.js
18
+ data.every((b) => b < 128)) {
19
+ // 为 ASCII 字符串优化
20
+ return decodeAscii(data);
21
+ }
22
+ // 使用系统解码
23
+ return textDecoder.decode(data);
24
+ }
15
25
  /** 特别优化字符串解码速度 */
16
26
  export class StringDecoder {
17
27
  constructor() {
@@ -30,15 +40,8 @@ export class StringDecoder {
30
40
  decode(data) {
31
41
  const length = data.byteLength;
32
42
  // 这里,length 类型为 Uint32
33
- if (length > 6) {
34
- if (length < 80 && // 只有小字符串有优化价值,见 benchmark-string.js
35
- data.every((b) => b < 128)) {
36
- // 为 ASCII 字符串优化
37
- return decodeAscii(data);
38
- }
39
- // 使用系统解码
40
- return textDecoder.decode(data);
41
- }
43
+ if (length > 6)
44
+ return decode(data);
42
45
  // 这里,length 类型为 0 | 1 | 2 | 3 | 4 | 5 | 6
43
46
  // 为小字符串优化
44
47
  if (length === 0)
@@ -51,12 +54,12 @@ export class StringDecoder {
51
54
  let key = 0;
52
55
  for (const byte of data) {
53
56
  key *= 256;
54
- key += byte | 0;
57
+ key += byte;
55
58
  }
56
59
  const match = cache.get(key);
57
60
  if (match != null)
58
61
  return match;
59
- const string = textDecoder.decode(data);
62
+ const string = decode(data);
60
63
  cache.set(key, string);
61
64
  return string;
62
65
  }
@@ -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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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.27",
3
+ "version": "0.3.0-alpha.28",
4
4
  "author": "CloudPSS",
5
5
  "license": "MIT",
6
6
  "keywords": [
@@ -16,6 +16,19 @@ const decodeAscii = (() => {
16
16
  return (array: Uint8Array): string => String.fromCharCode(...array);
17
17
  })();
18
18
 
19
+ /** 解码 */
20
+ function decode(data: Uint8Array): string {
21
+ if (
22
+ data.byteLength < 80 && // 只有小字符串有优化价值,见 benchmark-string.js
23
+ data.every((b) => b < 128)
24
+ ) {
25
+ // 为 ASCII 字符串优化
26
+ return decodeAscii(data);
27
+ }
28
+ // 使用系统解码
29
+ return textDecoder.decode(data);
30
+ }
31
+
19
32
  /** 特别优化字符串解码速度 */
20
33
  export class StringDecoder {
21
34
  /** 小字符串缓存 */
@@ -33,17 +46,8 @@ export class StringDecoder {
33
46
  decode(data: Uint8Array): string {
34
47
  const length = data.byteLength as 0 | 1 | 2 | 3 | 4 | 5 | 6;
35
48
  // 这里,length 类型为 Uint32
36
- if (length > 6) {
37
- if (
38
- length < 80 && // 只有小字符串有优化价值,见 benchmark-string.js
39
- data.every((b) => b < 128)
40
- ) {
41
- // 为 ASCII 字符串优化
42
- return decodeAscii(data);
43
- }
44
- // 使用系统解码
45
- return textDecoder.decode(data);
46
- }
49
+ if (length > 6) return decode(data);
50
+
47
51
  // 这里,length 类型为 0 | 1 | 2 | 3 | 4 | 5 | 6
48
52
  // 为小字符串优化
49
53
  if (length === 0) return '';
@@ -54,11 +58,11 @@ export class StringDecoder {
54
58
  let key = 0;
55
59
  for (const byte of data) {
56
60
  key *= 256;
57
- key += byte | 0;
61
+ key += byte;
58
62
  }
59
63
  const match = cache.get(key);
60
64
  if (match != null) return match;
61
- const string = textDecoder.decode(data);
65
+ const string = decode(data);
62
66
  cache.set(key, string);
63
67
  return string;
64
68
  }
@@ -12,6 +12,8 @@ test('decode string', (t) => {
12
12
  t.equal(decoder.decode(encoder.encode('ba')), 'ba');
13
13
  t.equal(decoder.decode(encoder.encode('123456')), '123456');
14
14
  t.equal(decoder.decode(encoder.encode('123465')), '123465');
15
+ t.equal(decoder.decode(encoder.encode('1234651')), '1234651');
16
+ t.equal(decoder.decode(encoder.encode('1234651')), '1234651');
15
17
 
16
18
  {
17
19
  // 检查所有单字节