@latticexyz/recs 2.0.0-next.3 → 2.0.0-next.4

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.
@@ -0,0 +1,152 @@
1
+ import { defineComponent as defineComponentV2, setComponent as setComponentV2 } from "./Component";
2
+ import { createWorld as createWorldV2 } from "./World";
3
+ import { createEntity as createEntityV2 } from "./Entity";
4
+ import { Type as TypeV2 } from "./constants";
5
+ import { HasValue as HasValueV2 } from "./Query";
6
+ import { defineSystem } from "./System";
7
+
8
+ export function timeIt(fn: () => unknown) {
9
+ const start = Date.now();
10
+ fn();
11
+ const end = Date.now();
12
+ const duration = end - start;
13
+ console.log("Duration:", duration);
14
+ return duration;
15
+ }
16
+
17
+ describe("V2", () => {
18
+ const size = 1000000;
19
+ it("measure creation of 1000000 entities", () => {
20
+ console.log("V2");
21
+ const world = createWorldV2();
22
+ const Position = defineComponentV2(world, { x: TypeV2.Number, y: TypeV2.Number });
23
+
24
+ timeIt(() => {
25
+ for (let i = 0; i < size; i++) {
26
+ const entity = createEntityV2(world);
27
+ setComponentV2(Position, entity, { x: 1, y: 1 });
28
+ }
29
+ });
30
+ });
31
+
32
+ it("measure creation of 1000000 entities and reacting to it", () => {
33
+ console.log("V2");
34
+ const world = createWorldV2();
35
+ const Position = defineComponentV2(world, { x: TypeV2.Number, y: TypeV2.Number });
36
+
37
+ defineSystem(world, [HasValueV2(Position, { x: 1, y: 1 })], (update) => {
38
+ const e = update;
39
+ });
40
+
41
+ timeIt(() => {
42
+ for (let i = 0; i < size; i++) {
43
+ const entity = createEntityV2(world);
44
+ setComponentV2(Position, entity, { x: 1, y: 1 });
45
+ }
46
+ });
47
+ });
48
+ });
49
+
50
+ describe("TypedArray", () => {
51
+ let array: number[];
52
+ let typedArray: Uint32Array;
53
+ const size = 10000000;
54
+
55
+ beforeAll(() => {
56
+ // Set up array
57
+ array = [];
58
+ const randomOtherArr: number[] = [];
59
+ for (let i = 0; i < size; i++) {
60
+ array.push(i);
61
+ randomOtherArr.push(i);
62
+ }
63
+
64
+ // Set up buffer
65
+ const buffer = new ArrayBuffer(4 * size);
66
+ typedArray = new Uint32Array(buffer);
67
+
68
+ for (let i = 0; i < size; i++) {
69
+ typedArray[i] = i;
70
+ }
71
+ });
72
+
73
+ it.skip("measure time to create 10 million entite", () => {
74
+ console.log("Sparse array");
75
+ const sparse: number[] = [];
76
+ timeIt(() => {
77
+ for (let i = 0; i < size * 100; i += 100) {
78
+ sparse[i] = 0;
79
+ }
80
+ });
81
+
82
+ console.log("Dense array push");
83
+ const dense1: number[] = [];
84
+ timeIt(() => {
85
+ for (let i = 0; i < size * 100; i += 100) {
86
+ dense1.push(i);
87
+ }
88
+ });
89
+
90
+ console.log("Dense array set");
91
+ const dense2: number[] = [];
92
+ timeIt(() => {
93
+ for (let i = 0; i < size; i++) {
94
+ dense2[i] = i;
95
+ }
96
+ });
97
+
98
+ console.log("Map");
99
+ const map = new Map<number, number>();
100
+ timeIt(() => {
101
+ for (let i = 0; i < size * 100; i += 100) {
102
+ map.set(i, i);
103
+ }
104
+ });
105
+
106
+ console.log("Object");
107
+ const obj: { [key: number]: number } = {};
108
+ timeIt(() => {
109
+ for (let i = 0; i < size * 100; i += 100) {
110
+ obj[i] = i;
111
+ }
112
+ });
113
+ });
114
+
115
+ it.skip("measure time to iterate through regular array with 10 million entries", () => {
116
+ console.log("Regular array:");
117
+ timeIt(() => {
118
+ let sum = 0;
119
+ for (let i = 0; i < size; i++) {
120
+ sum += i;
121
+ }
122
+ console.log("Sum", sum);
123
+ });
124
+
125
+ console.log("Regular array iterator:");
126
+ timeIt(() => {
127
+ let sum = 0;
128
+ for (const i of array) {
129
+ sum += i;
130
+ }
131
+ console.log("Sum", sum);
132
+ });
133
+
134
+ console.log("Typed array:");
135
+ timeIt(() => {
136
+ let sum = 0;
137
+ for (let i = 0; i < size; i++) {
138
+ sum += typedArray[i];
139
+ }
140
+ console.log("Sum", sum);
141
+ });
142
+
143
+ console.log("Typed array iterator:");
144
+ timeIt(() => {
145
+ let sum = 0;
146
+ for (const i of typedArray) {
147
+ sum += i;
148
+ }
149
+ console.log("Sum", sum);
150
+ });
151
+ });
152
+ });