-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathperformance.html
114 lines (84 loc) · 2.98 KB
/
performance.html
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<!DOCTYPE html>
<html>
<head>
<title>Redux basic example</title>
<script src="https://unpkg.com/redux@latest/dist/redux.min.js"></script>
</head>
<body>
<div>
<p>
<span id="value-redux">0</span> times
<span id="value-gordux">0</span> times
</p>
</div>
<script>
// ACTIONS (Shared)
function counter(state, action) {
if (typeof state === 'undefined') {
return 0
}
switch (action.type) {
case 'INCREMENT':
return state + 1
case 'DECREMENT':
return state - 1
default:
return state
}
}
// STORE REDUX
var storeRedux = Redux.createStore(counter)
// STORE GORDUX
var storeGordux = 0;
document.addEventListener('action', function(e) {
storeGordux = counter(storeGordux, e.detail);
document.dispatchEvent(new CustomEvent('state'));
}, false);
// RENDER REDUX UPDATE
var valueRexdux = document.getElementById('value-redux');
function renderRedux() {
valueRexdux.innerHTML = storeRedux.getState().toString()
}
storeRedux.subscribe(renderRedux);
renderRedux();
// RENDER GORDUX UPDATE
var valueGordux = document.getElementById('value-gordux');
function renderGordux() {
valueGordux.innerHTML = storeGordux.toString()
}
document.addEventListener('state', renderGordux);
renderGordux();
// TEST CONFIG
iterationCount = 100;
// START REDUX UPDATE
storeRedux.subscribe(function() {
// console.log('Redux updated to ' + storeRedux.getState());
if (storeRedux.getState() == iterationCount) {
t1 = performance.now();
document.write("<p>Redux took " + (t1 - t0).toFixed(3) + " milliseconds ("+iterationCount +" iterations).</p>");
}
});
t0 = performance.now();
for(var i = 0; i < iterationCount; ++i) {
storeRedux.dispatch({ type: 'INCREMENT' });
}
// START GORDUX UPDATE
document.addEventListener('state', function() {
// console.log('Gordux updated to ' + storeGordux);
if (storeGordux == iterationCount) {
t3 = performance.now();
document.write("<p>Gordux took " + (t3 - t2).toFixed(3) + " milliseconds ("+iterationCount +" iterations).</p>");
document.write("<p>Gordux " + ((t3 - t2) / (t1 - t0)).toFixed(1) + " x " + " vs redux</p>");
}
});
t2 = performance.now();
for(var i = 0; i < iterationCount; ++i) {
document.dispatchEvent(
new CustomEvent('action', { detail:
{ type: 'INCREMENT' }
})
);
}
</script>
</body>
</html>