-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.ts
82 lines (67 loc) · 1.49 KB
/
benchmark.ts
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
import b from "benny";
import { superstate } from "./src/index.mjs";
import { createMachine, createActor } from "xstate";
b.suite(
"Creating factory",
b.add("Superstate", () => {
superstate("toggle")
.state("inactive", "toggle() -> active")
.state("active", "toggle() -> inactive");
}),
b.add("XState", () => {
createMachine({
id: "toggle",
initial: "Inactive",
states: {
Inactive: {
on: { toggle: "Active" },
},
Active: {
on: { toggle: "Inactive" },
},
},
});
}),
b.cycle(),
b.complete()
);
const superstateToggle = superstate<"inactive" | "active">("toggle")
.state("inactive", "toggle() -> active")
.state("active", "toggle() -> inactive");
const xstateToggle = createMachine({
id: "toggle",
initial: "Inactive",
states: {
Inactive: {
on: { toggle: "Active" },
},
Active: {
on: { toggle: "Inactive" },
},
},
});
b.suite(
"Creating instance",
b.add("Superstate", () => {
superstateToggle.host();
}),
b.add("XState", () => {
const actor = createActor(xstateToggle);
actor.start();
}),
b.cycle(),
b.complete()
);
const superstateInstance = superstateToggle.host();
const xstateInstance = createActor(xstateToggle).start();
b.suite(
"Sending events",
b.add("Superstate", () => {
superstateInstance.send.toggle();
}),
b.add("XState", () => {
xstateInstance.send({ type: "toggle" });
}),
b.cycle(),
b.complete()
);