json-as 1.0.0-beta.16 → 1.0.0-beta.18

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.
Files changed (62) hide show
  1. package/{CHANGELOG → CHANGELOG.md} +11 -1
  2. package/README.md +80 -66
  3. package/assembly/__benches__/abc.bench.ts +22 -0
  4. package/assembly/__benches__/large.bench.ts +174 -0
  5. package/assembly/__benches__/medium.bench.ts +46 -0
  6. package/assembly/__benches__/small.bench.ts +33 -0
  7. package/assembly/__benches__/vec3.bench.ts +73 -0
  8. package/assembly/__tests__/arbitrary.spec.ts +3 -3
  9. package/assembly/__tests__/array.spec.ts +5 -8
  10. package/assembly/__tests__/box.spec.ts +10 -20
  11. package/assembly/__tests__/custom.spec.ts +7 -6
  12. package/assembly/__tests__/date.spec.ts +4 -6
  13. package/assembly/__tests__/float.spec.ts +3 -3
  14. package/assembly/__tests__/lib/index.ts +4 -0
  15. package/assembly/__tests__/map.spec.ts +1 -1
  16. package/assembly/__tests__/misc.spec.ts +90 -0
  17. package/assembly/__tests__/raw.spec.ts +3 -3
  18. package/assembly/__tests__/struct.spec.ts +3 -6
  19. package/assembly/custom/bench.ts +9 -2
  20. package/assembly/deserialize/simple/array/struct.ts +2 -3
  21. package/assembly/deserialize/simple/array.ts +1 -1
  22. package/assembly/deserialize/simple/bool.ts +1 -1
  23. package/assembly/deserialize/simple/map.ts +3 -4
  24. package/assembly/deserialize/simple/object.ts +2 -3
  25. package/assembly/deserialize/simple/raw.ts +1 -1
  26. package/assembly/deserialize/simple/struct.ts +9 -5
  27. package/assembly/index.ts +14 -5
  28. package/assembly/serialize/simple/arbitrary.ts +1 -0
  29. package/assembly/serialize/simple/array.ts +1 -0
  30. package/assembly/serialize/simple/bool.ts +1 -0
  31. package/assembly/serialize/simple/date.ts +1 -0
  32. package/assembly/serialize/simple/float.ts +1 -0
  33. package/assembly/serialize/simple/integer.ts +2 -1
  34. package/assembly/serialize/simple/map.ts +1 -0
  35. package/assembly/serialize/simple/object.ts +4 -3
  36. package/assembly/serialize/simple/raw.ts +1 -0
  37. package/assembly/serialize/simple/string.ts +1 -0
  38. package/assembly/test.ts +63 -39
  39. package/bench/abc.bench.ts +20 -0
  40. package/bench/large.bench.ts +126 -0
  41. package/bench/medium.bench.ts +43 -0
  42. package/bench/small.bench.ts +30 -0
  43. package/bench/vec3.bench.ts +26 -0
  44. package/index.ts +1 -1
  45. package/{lib → modules}/as-bs.ts +4 -13
  46. package/package.json +5 -3
  47. package/run-bench.as.sh +15 -0
  48. package/run-bench.js.sh +12 -0
  49. package/run-tests.sh +1 -1
  50. package/transform/lib/index.js +26 -46
  51. package/transform/lib/index.js.map +1 -1
  52. package/transform/src/index.ts +31 -72
  53. package/assembly/__benches__/misc.bench.ts +0 -47
  54. package/assembly/__benches__/schemas.ts +0 -25
  55. package/assembly/__benches__/string.bench.ts +0 -23
  56. package/assembly/__benches__/struct.bench.ts +0 -21
  57. package/assembly/as-bs.d.ts +0 -53
  58. package/bench/schemas.ts +0 -5
  59. package/bench/string.bench.ts +0 -16
  60. package/bench.js +0 -85
  61. /package/bench/{bench.ts → lib/bench.ts} +0 -0
  62. /package/{lib → modules}/tsconfig.json +0 -0
@@ -1,3 +1,4 @@
1
+ import { bs } from "../../../modules/as-bs";
1
2
  import { dtoa_buffered } from "util/number";
2
3
 
3
4
  export function serializeFloat<T extends number>(data: T): void {
@@ -1,3 +1,4 @@
1
+ import { bs } from "../../../modules/as-bs";
1
2
  import { itoa_buffered } from "util/number";
2
3
 
3
4
  export function serializeInteger<T extends number>(data: T): void {
@@ -8,4 +9,4 @@ export function serializeInteger<T extends number>(data: T): void {
8
9
  }
9
10
 
10
11
  // 32 {"x":,"y":,"z"}
11
- // 18 3.41.28.3
12
+ // 18 3.41.28.3
@@ -1,3 +1,4 @@
1
+ import { bs } from "../../../modules/as-bs";
1
2
  import { JSON } from "../..";
2
3
  import { BRACE_LEFT, BRACE_RIGHT, COLON, COMMA } from "../../custom/chars";
3
4
 
@@ -1,3 +1,4 @@
1
+ import { bs } from "../../../modules/as-bs";
1
2
  import { JSON } from "../..";
2
3
  import { BRACE_LEFT, BRACE_RIGHT, QUOTE } from "../../custom/chars";
3
4
  import { bytes } from "../../util";
@@ -24,7 +25,7 @@ export function serializeObject(data: JSON.Obj): void {
24
25
  const keySize = bytes(firstKey);
25
26
  store<u16>(bs.offset, QUOTE);
26
27
  memory.copy(bs.offset + 2, changetype<usize>(firstKey), keySize);
27
- store<u32>(bs.offset += keySize + 2, 3801122); // ":
28
+ store<u32>((bs.offset += keySize + 2), 3801122); // ":
28
29
  bs.offset += 4;
29
30
  JSON.__serialize(unchecked(values[0]));
30
31
 
@@ -33,11 +34,11 @@ export function serializeObject(data: JSON.Obj): void {
33
34
  const keySize = bytes(key);
34
35
  store<u32>(bs.offset, 2228268); // ,"
35
36
  memory.copy(bs.offset + 4, changetype<usize>(key), keySize);
36
- store<u32>(bs.offset += keySize + 4, 3801122); // ":
37
+ store<u32>((bs.offset += keySize + 4), 3801122); // ":
37
38
  bs.offset += 4;
38
39
  JSON.__serialize(unchecked(values[i]));
39
40
  }
40
41
 
41
42
  store<u16>(bs.offset, BRACE_RIGHT);
42
43
  bs.offset += 2;
43
- }
44
+ }
@@ -1,3 +1,4 @@
1
+ import { bs } from "../../../modules/as-bs";
1
2
  import { JSON } from "../..";
2
3
  import { bytes } from "../../util";
3
4
 
@@ -1,3 +1,4 @@
1
+ import { bs } from "../../../modules/as-bs";
1
2
  import { _intTo16 } from "../../custom/util";
2
3
  import { bytes } from "../../util/bytes";
3
4
  import { BACK_SLASH, QUOTE } from "../../custom/chars";
package/assembly/test.ts CHANGED
@@ -1,13 +1,15 @@
1
1
  import { JSON } from ".";
2
2
  import { bytes } from "./util";
3
3
 
4
+
4
5
  @json
5
6
  class Obj {
6
7
  public a: string = "hello";
7
8
  public b: string = "world";
8
- public c: string = "\"\t\f\u0000\u0001";
9
+ public c: string = '"\t\f\u0000\u0001';
9
10
  }
10
11
 
12
+
11
13
  @json
12
14
  class Vec3 {
13
15
  x: f32 = 0.0;
@@ -15,8 +17,10 @@ class Vec3 {
15
17
  z: f32 = 0.0;
16
18
  }
17
19
 
20
+
18
21
  @json
19
22
  class Player {
23
+
20
24
  @alias("first name")
21
25
  firstName!: string;
22
26
  lastName!: string;
@@ -24,11 +28,14 @@ class Player {
24
28
  // Drop in a code block, function, or expression that evaluates to a boolean
25
29
  @omitif((self: Player) => self.age < 18)
26
30
  age!: i32;
31
+
32
+
27
33
  @omitnull()
28
34
  pos!: Vec3 | null;
29
35
  isVerified!: boolean;
30
36
  }
31
37
 
38
+
32
39
  @json
33
40
  class Point {
34
41
  x: f64 = 0.0;
@@ -37,10 +44,14 @@ class Point {
37
44
  this.x = x;
38
45
  this.y = y;
39
46
  }
47
+
48
+
40
49
  @serializer
41
50
  serializer(self: Point): string {
42
51
  return `(${self.x},${self.y})`;
43
52
  }
53
+
54
+
44
55
  @deserializer
45
56
  deserializer(data: string): Point | null {
46
57
  const dataSize = bytes(data);
@@ -50,13 +61,22 @@ class Point {
50
61
  const x = data.slice(1, c);
51
62
  const y = data.slice(c + 1, data.length - 1);
52
63
 
53
- return new Point(
54
- f64.parse(x),
55
- f64.parse(y)
56
- );
64
+ return new Point(f64.parse(x), f64.parse(y));
57
65
  }
58
66
  }
59
67
 
68
+
69
+ @json
70
+ class InnerObj<T> {
71
+ obj: T = instantiate<T>();
72
+ }
73
+
74
+
75
+ @json
76
+ class ObjWithBracketString {
77
+ data: string = "";
78
+ }
79
+
60
80
  const player: Player = {
61
81
  firstName: "Jairus",
62
82
  lastName: "Tanaka",
@@ -65,62 +85,66 @@ const player: Player = {
65
85
  pos: {
66
86
  x: 3.4,
67
87
  y: 1.2,
68
- z: 8.3
88
+ z: 8.3,
69
89
  },
70
- isVerified: true
90
+ isVerified: true,
71
91
  };
72
92
 
73
- // const a1 = JSON.stringify("\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u0009\u000a\u000b\u000c\u000d\u000e\u000f\u000f\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f");
74
- // // console.log("Bytes " + bytes(a1).toString());
75
- // console.log("a1: " + a1);
93
+ const a1 = JSON.stringify("\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u0009\u000a\u000b\u000c\u000d\u000e\u000f\u000f\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f");
94
+ // console.log("Bytes " + bytes(a1).toString());
95
+ console.log("a1: " + a1);
76
96
 
77
- // const obj = new Obj();
78
- // const a2 = JSON.stringify(obj);
79
- // // console.log("Bytes " + bytes(a2).toString());
80
- // console.log("a2: " + a2);
97
+ const obj = new Obj();
98
+ const a2 = JSON.stringify(obj);
99
+ // console.log("Bytes " + bytes(a2).toString());
100
+ console.log("a2: " + a2);
81
101
 
82
- // const a3 = JSON.stringify(player);
83
- // // console.log("Bytes " + bytes(a3).toString());
84
- // console.log("a3: " + a3);
102
+ const a3 = JSON.stringify(player);
103
+ // console.log("Bytes " + bytes(a3).toString());
104
+ console.log("a3: " + a3);
85
105
 
86
- // const a4 = new JSON.Obj();
106
+ const a4 = new JSON.Obj();
87
107
 
88
- // a4.set("x", 1.5);
89
- // a4.set("y", 5.4);
90
- // a4.set("z", 9.8);
91
- // a4.set("obj", obj)
92
- // a4.set<boolean>("bool", false);
108
+ a4.set("x", 1.5);
109
+ a4.set("y", 5.4);
110
+ a4.set("z", 9.8);
111
+ a4.set("obj", obj);
112
+ a4.set<boolean>("bool", false);
93
113
 
94
- // console.log("a4: " + JSON.stringify(a4));
114
+ console.log("a4: " + JSON.stringify(a4));
95
115
 
96
- // const a5 = JSON.parse<JSON.Obj>('{"foo":"bar"}');
116
+ const a5 = JSON.parse<JSON.Obj>('{"foo":"bar"}');
97
117
 
98
- // console.log("a5: " + JSON.stringify(a5));
118
+ console.log("a5: " + JSON.stringify(a5));
99
119
 
100
- // const a6 = JSON.parse<JSON.Obj>('{"x":1.5,"y":5.4,"z":9.8,"obj":{"foo":"bar"}}');
120
+ const a6 = JSON.parse<JSON.Obj>('{"x":1.5,"y":5.4,"z":9.8,"obj":{"foo":"bar"}}');
101
121
 
102
- // console.log("a6: " + JSON.stringify(a6));
122
+ console.log("a6: " + JSON.stringify(a6));
103
123
 
104
- // const a7 = JSON.parse<JSON.Value[]>('["string",true,3.14,{"x":1.0,"y":2.0,"z":3.0},[1,2,3,true]]');
124
+ const a7 = JSON.parse<JSON.Value[]>('["string",true,3.14,{"x":1.0,"y":2.0,"z":3.0},[1,2,3,true]]');
105
125
 
106
- // console.log("a7: " + JSON.stringify(a7));
126
+ console.log("a7: " + JSON.stringify(a7));
107
127
 
108
- // const a8 = JSON.stringify(["hello", JSON.stringify("world"),"working?"]);
128
+ const a8 = JSON.stringify(["hello", JSON.stringify("world"), "working?"]);
109
129
 
110
- // console.log("a8: " + a8);
130
+ console.log("a8: " + a8);
111
131
 
112
- // const a9 = JSON.stringify<JSON.Raw>(JSON.Raw.from("\"hello world\""));
132
+ const a9 = JSON.stringify<JSON.Raw>(JSON.Raw.from('"hello world"'));
113
133
 
114
- // console.log("a9: " + a9);
134
+ console.log("a9: " + a9);
115
135
 
116
- // const m10 = new Map<string, JSON.Raw>();
117
- // m10.set("hello", new JSON.Raw("\"world\""));
118
- // m10.set("pos", new JSON.Raw("{\"x\":1.0,\"y\":2.0,\"z\":3.0}"));
136
+ const m10 = new Map<string, JSON.Raw>();
137
+ m10.set("hello", new JSON.Raw('"world"'));
138
+ m10.set("pos", new JSON.Raw('{"x":1.0,"y":2.0,"z":3.0}'));
119
139
 
120
- // const a10 = JSON.stringify(m10);
140
+ const a10 = JSON.stringify(m10);
121
141
 
122
- // console.log("a10: " + a10);
142
+ console.log("a10: " + a10);
123
143
 
124
144
  const a11 = JSON.parse<Vec3>(' { "x" : 3.4 , "y" : 1.2 , "z" : 8.3 } ');
125
145
 
126
146
  console.log("a11: " + JSON.stringify(a11));
147
+
148
+ const a12 = JSON.parse<InnerObj<ObjWithBracketString>>('{"obj":{"data":"hello} world"}}');
149
+
150
+ console.log("a12: " + JSON.stringify(a12));
@@ -0,0 +1,20 @@
1
+ import { bench } from "./lib/bench";
2
+
3
+ const v1 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
4
+ const v2 = '"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"';
5
+
6
+ bench(
7
+ "Serialize Alphabet",
8
+ () => {
9
+ JSON.stringify(v1);
10
+ },
11
+ 64_000_00,
12
+ );
13
+
14
+ bench(
15
+ "Deserialize Alphabet",
16
+ () => {
17
+ JSON.parse(v2);
18
+ },
19
+ 64_000_00,
20
+ );
@@ -0,0 +1,126 @@
1
+ import { bench } from "./lib/bench";
2
+
3
+ class Vec3 {
4
+ public x!: number;
5
+ public y!: number;
6
+ public z!: number;
7
+ }
8
+
9
+ class LargeJSON {
10
+ public id!: number;
11
+ public name!: string;
12
+ public age!: number;
13
+ public email!: string;
14
+ public street!: string;
15
+ public city!: string;
16
+ public state!: string;
17
+ public zip!: string;
18
+ public tags!: string[];
19
+ public theme!: string;
20
+ public notifications!: boolean;
21
+ public language!: string;
22
+ public movement!: Vec3[];
23
+ }
24
+
25
+ const v1: LargeJSON = {
26
+ id: 2,
27
+ name: "Medium Object",
28
+ age: 18,
29
+ email: "me@jairus.dev",
30
+ street: "I don't want to say my street",
31
+ city: "I don't want to say this either",
32
+ state: "It really depends",
33
+ zip: "I forget what it is",
34
+ tags: ["me", "dogs", "mountains", "bar", "foo"],
35
+ theme: "Hyper Term Black",
36
+ notifications: true,
37
+ language: "en-US",
38
+ movement: [
39
+ { x: 1, y: 2, z: 3 },
40
+ { x: 1, y: 2, z: 3 },
41
+ { x: 1, y: 2, z: 3 },
42
+ { x: 1, y: 2, z: 3 },
43
+ { x: 1, y: 2, z: 3 },
44
+ { x: 1, y: 2, z: 3 },
45
+ { x: 1, y: 2, z: 3 },
46
+ { x: 1, y: 2, z: 3 },
47
+ { x: 1, y: 2, z: 3 },
48
+ { x: 1, y: 2, z: 3 },
49
+ { x: 1, y: 2, z: 3 },
50
+ { x: 1, y: 2, z: 3 },
51
+ { x: 1, y: 2, z: 3 },
52
+ { x: 1, y: 2, z: 3 },
53
+ { x: 1, y: 2, z: 3 },
54
+ { x: 1, y: 2, z: 3 },
55
+ { x: 1, y: 2, z: 3 },
56
+ { x: 1, y: 2, z: 3 },
57
+ { x: 1, y: 2, z: 3 },
58
+ { x: 1, y: 2, z: 3 },
59
+ { x: 1, y: 2, z: 3 },
60
+ { x: 1, y: 2, z: 3 },
61
+ { x: 1, y: 2, z: 3 },
62
+ { x: 1, y: 2, z: 3 },
63
+ { x: 1, y: 2, z: 3 },
64
+ { x: 1, y: 2, z: 3 },
65
+ { x: 1, y: 2, z: 3 },
66
+ { x: 1, y: 2, z: 3 },
67
+ { x: 1, y: 2, z: 3 },
68
+ { x: 1, y: 2, z: 3 },
69
+ { x: 1, y: 2, z: 3 },
70
+ { x: 1, y: 2, z: 3 },
71
+ { x: 1, y: 2, z: 3 },
72
+ { x: 1, y: 2, z: 3 },
73
+ { x: 1, y: 2, z: 3 },
74
+ { x: 1, y: 2, z: 3 },
75
+ { x: 1, y: 2, z: 3 },
76
+ { x: 1, y: 2, z: 3 },
77
+ { x: 1, y: 2, z: 3 },
78
+ { x: 1, y: 2, z: 3 },
79
+ { x: 1, y: 2, z: 3 },
80
+ { x: 1, y: 2, z: 3 },
81
+ { x: 1, y: 2, z: 3 },
82
+ { x: 1, y: 2, z: 3 },
83
+ { x: 1, y: 2, z: 3 },
84
+ { x: 1, y: 2, z: 3 },
85
+ { x: 1, y: 2, z: 3 },
86
+ { x: 1, y: 2, z: 3 },
87
+ { x: 1, y: 2, z: 3 },
88
+ { x: 1, y: 2, z: 3 },
89
+ { x: 1, y: 2, z: 3 },
90
+ { x: 1, y: 2, z: 3 },
91
+ { x: 1, y: 2, z: 3 },
92
+ { x: 1, y: 2, z: 3 },
93
+ { x: 1, y: 2, z: 3 },
94
+ { x: 1, y: 2, z: 3 },
95
+ { x: 1, y: 2, z: 3 },
96
+ { x: 1, y: 2, z: 3 },
97
+ { x: 1, y: 2, z: 3 },
98
+ { x: 1, y: 2, z: 3 },
99
+ { x: 1, y: 2, z: 3 },
100
+ { x: 1, y: 2, z: 3 },
101
+ { x: 1, y: 2, z: 3 },
102
+ { x: 1, y: 2, z: 3 },
103
+ { x: 1, y: 2, z: 3 },
104
+ { x: 1, y: 2, z: 3 },
105
+ { x: 1, y: 2, z: 3 },
106
+ { x: 1, y: 2, z: 3 },
107
+ ],
108
+ };
109
+
110
+ const v2 = `{"id":2,"name":"Medium Object","age":18,"email":"me@jairus.dev","street":"I don't want to say my street","city":"I don't want to say this either","state":"It really depends","zip":"I forget what it is","tags":["me","dogs","mountains","bar","foo"],"theme":"Hyper Term Black","notifications":true,"language":"en-US","movement":[{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3},{"x":1,"y":2,"z":3}]}`;
111
+
112
+ bench(
113
+ "Serialize Medium Object",
114
+ () => {
115
+ JSON.stringify(v1);
116
+ },
117
+ 3_000_00,
118
+ );
119
+
120
+ bench(
121
+ "Deserialize Medium Object",
122
+ () => {
123
+ JSON.parse(v2);
124
+ },
125
+ 3_000_00,
126
+ );
@@ -0,0 +1,43 @@
1
+ import { bench } from "./lib/bench";
2
+
3
+ class MediumJSON {
4
+ public id!: number;
5
+ public name!: string;
6
+ public age!: number;
7
+ public email!: string;
8
+ public street!: string;
9
+ public city!: string;
10
+ public state!: string;
11
+ public zip!: string;
12
+ public tags!: string[];
13
+ }
14
+
15
+ const v1: MediumJSON = {
16
+ id: 2,
17
+ name: "Medium Object",
18
+ age: 18,
19
+ email: "me@jairus.dev",
20
+ street: "I don't want to say my street",
21
+ city: "I don't want to say this either",
22
+ state: "It really depends",
23
+ zip: "I forget what it is",
24
+ tags: ["me", "dogs", "mountains", "bar", "foo"],
25
+ };
26
+
27
+ const v2 = `{"id":2,"name":"Medium Object","age":18,"email":"me@jairus.dev","street":"I don't want to say my street","city":"I don't want to say this either","state":"It really depends","zip":"I forget what it is","tags":["me","dogs","mountains","bar","foo"]}`;
28
+
29
+ bench(
30
+ "Serialize Medium Object",
31
+ () => {
32
+ JSON.stringify(v1);
33
+ },
34
+ 8_000_00,
35
+ );
36
+
37
+ bench(
38
+ "Deserialize Medium Object",
39
+ () => {
40
+ JSON.parse(v2);
41
+ },
42
+ 8_000_00,
43
+ );
@@ -0,0 +1,30 @@
1
+ import { bench } from "./lib/bench";
2
+
3
+ class SmallJSON {
4
+ public id!: number;
5
+ public name!: string;
6
+ public active!: boolean;
7
+ }
8
+
9
+ const v1: SmallJSON = {
10
+ id: 1,
11
+ name: "Small Object",
12
+ active: true,
13
+ };
14
+ const v2 = '{"id":1,"name":"Small Object","active":true}';
15
+
16
+ bench(
17
+ "Serialize Small Object",
18
+ () => {
19
+ JSON.stringify(v1);
20
+ },
21
+ 16_000_00,
22
+ );
23
+
24
+ bench(
25
+ "Deserialize Small Object",
26
+ () => {
27
+ JSON.parse(v2);
28
+ },
29
+ 16_000_00,
30
+ );
@@ -0,0 +1,26 @@
1
+ import { bench } from "./lib/bench";
2
+
3
+ class Vec3 {
4
+ public x!: number;
5
+ public y!: number;
6
+ public z!: number;
7
+ }
8
+
9
+ const v1: Vec3 = { x: 1, y: 2, z: 3 };
10
+ const v2 = '{"x":1,"y":2,"z":3}';
11
+
12
+ bench(
13
+ "Serialize Vec3",
14
+ () => {
15
+ JSON.stringify(v1);
16
+ },
17
+ 16_000_00,
18
+ );
19
+
20
+ bench(
21
+ "Deserialize Vec3",
22
+ () => {
23
+ JSON.parse(v2);
24
+ },
25
+ 16_000_00,
26
+ );
package/index.ts CHANGED
@@ -1 +1 @@
1
- export * from "./assembly/index";
1
+ export * from "./assembly/";
@@ -5,7 +5,7 @@ import { OBJECT, TOTAL_OVERHEAD } from "rt/common";
5
5
  */
6
6
  export namespace bs {
7
7
  /** Current buffer pointer. */ // @ts-ignore
8
- export let buffer: ArrayBuffer = new ArrayBuffer(32);//__new(32, idof<ArrayBuffer>());
8
+ export let buffer: ArrayBuffer = new ArrayBuffer(32); //__new(32, idof<ArrayBuffer>());
9
9
 
10
10
  /** Current offset within the buffer. */
11
11
  export let offset: usize = changetype<usize>(buffer);
@@ -28,10 +28,7 @@ export namespace bs {
28
28
  const deltaBytes = nextPowerOf2(size + 64);
29
29
  bufferSize += deltaBytes;
30
30
  // @ts-ignore: exists
31
- const newPtr = changetype<ArrayBuffer>(__renew(
32
- changetype<usize>(buffer),
33
- bufferSize
34
- ));
31
+ const newPtr = changetype<ArrayBuffer>(__renew(changetype<usize>(buffer), bufferSize));
35
32
  offset = offset + changetype<usize>(newPtr) - changetype<usize>(buffer);
36
33
  buffer = newPtr;
37
34
  }
@@ -49,10 +46,7 @@ export namespace bs {
49
46
  const deltaBytes = nextPowerOf2(size);
50
47
  bufferSize += deltaBytes;
51
48
  // @ts-ignore: exists
52
- const newPtr = changetype<ArrayBuffer>(__renew(
53
- changetype<usize>(buffer),
54
- bufferSize
55
- ));
49
+ const newPtr = changetype<ArrayBuffer>(__renew(changetype<usize>(buffer), bufferSize));
56
50
  offset = offset + changetype<usize>(newPtr) - changetype<usize>(buffer);
57
51
  buffer = newPtr;
58
52
  }
@@ -70,10 +64,7 @@ export namespace bs {
70
64
  const deltaBytes = nextPowerOf2(size + 64);
71
65
  bufferSize += deltaBytes;
72
66
  // @ts-ignore
73
- const newPtr = changetype<ArrayBuffer>(__renew(
74
- changetype<usize>(buffer),
75
- bufferSize
76
- ));
67
+ const newPtr = changetype<ArrayBuffer>(__renew(changetype<usize>(buffer), bufferSize));
77
68
  // if (buffer != newPtr) console.log(" Old: " + changetype<usize>(buffer).toString() + "\n New: " + changetype<usize>(newPtr).toString());
78
69
  offset = offset + changetype<usize>(newPtr) - changetype<usize>(buffer);
79
70
  buffer = newPtr;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "1.0.0-beta.16",
3
+ "version": "1.0.0-beta.18",
4
4
  "author": "Jairus Tanaka",
5
5
  "repository": {
6
6
  "type": "git",
@@ -16,6 +16,7 @@
16
16
  "assemblyscript": "^0.27.34",
17
17
  "assemblyscript-prettier": "^3.0.1",
18
18
  "prettier": "^3.5.0",
19
+ "tsx": "^4.19.3",
19
20
  "typescript": "^5.7.3"
20
21
  },
21
22
  "bugs": {
@@ -57,8 +58,9 @@
57
58
  },
58
59
  "scripts": {
59
60
  "test": "bash ./run-tests.sh",
60
- "build:bench": "rm -rf ./build/ && JSON_DEBUG=true asc --lib ./lib/ assembly/__benches__/string.bench.ts -o ./build/bench.wasm --textFile ./build/bench.wat --transform ./transform --optimizeLevel 3 --shrinkLevel 0 --converge --noAssert --uncheckedBehavior always --runtime stub --enable simd --enable bulk-memory",
61
- "build:test": "rm -rf ./build/ && JSON_DEBUG=true asc --lib ./lib/ assembly/test.ts --transform ./transform -o ./build/test.wasm --textFile ./build/test.wat --optimizeLevel 3 --shrinkLevel 0",
61
+ "bench:as": "bash ./run-bench.as.sh",
62
+ "bench:js": "bash ./run-bench.js.sh",
63
+ "build:test": "rm -rf ./build/ && JSON_DEBUG=true asc assembly/test.ts --transform ./transform -o ./build/test.wasm --textFile ./build/test.wat --optimizeLevel 3 --shrinkLevel 0 --runtime stub",
62
64
  "build:test:simd": "rm -rf ./build/ && JSON_DEBUG=true asc assembly/test.ts --transform ./transform -o ./build/test.wasm --textFile ./build/test.wat --optimizeLevel 3 --shrinkLevel 0 --enable simd",
63
65
  "test:wasmtime": "wasmtime ./build/test.wasm",
64
66
  "test:wasmer": "wasmer ./build/test.wasm",
@@ -0,0 +1,15 @@
1
+ #!/bin/bash
2
+
3
+ mkdir -p ./build
4
+
5
+ for file in ./assembly/__benches__/*.bench.ts; do
6
+ filename=$(basename -- "$file")
7
+ output="./build/${filename%.ts}.wasm"
8
+
9
+ npx asc "$file" --transform ./transform -o "$output" --optimizeLevel 3 --shrinkLevel 0 --converge --noAssert --uncheckedBehavior always --runtime stub --enable simd --enable bulk-memory || { echo "Build failed"; exit 1; }
10
+
11
+ echo -e "$filename\n"
12
+ wasmer "$output" --llvm || { echo "Benchmarked failed."; exit 1; }
13
+ done
14
+
15
+ echo "Finished benchmarks."
@@ -0,0 +1,12 @@
1
+ #!/bin/bash
2
+
3
+ mkdir -p ./build
4
+
5
+ for file in ./bench/*.bench.ts; do
6
+ filename=$(basename -- "$file")
7
+
8
+ echo -e "$filename\n"
9
+ npx tsx "$file" --experimental-strip-types || { echo "Benchmark failed."; exit 1; }
10
+ done
11
+
12
+ echo "Finished benchmarks."
package/run-tests.sh CHANGED
@@ -6,7 +6,7 @@ for file in ./assembly/__tests__/*.spec.ts; do
6
6
  filename=$(basename -- "$file")
7
7
  output="./build/${filename%.ts}.wasm"
8
8
 
9
- asc "$file" --transform ./transform --lib ./lib/ -o "$output" || { echo "Tests failed"; exit 1; }
9
+ npx asc "$file" --transform ./transform -o "$output" || { echo "Tests failed"; exit 1; }
10
10
 
11
11
  echo " -> $filename"
12
12
  wasmtime "$output" || { echo "Tests failed"; exit 1; }