/
app.js
198 lines (163 loc) · 4.94 KB
/
app.js
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { linkEvent, version, render } from 'inferno';
uibench.init('Inferno', version);
function shouldDataUpdate(lastProps, nextProps) {
return lastProps !== nextProps;
}
const SCU_hooks = {
onComponentShouldUpdate: shouldDataUpdate
};
function TreeLeaf({ children }) {
return (
<li $HasTextChildren className="TreeLeaf">
{children}
</li>
);
}
TreeLeaf.defaultHooks = SCU_hooks;
function TreeNode({ data }) {
var length = data.children.length;
var children = new Array(length);
for (var i = 0; i < length; i++) {
var n = data.children[i];
var id = n.id;
if (n.container) {
children[i] = <TreeNode data={n} key={id} />;
} else {
children[i] = <TreeLeaf key={id}>{id}</TreeLeaf>;
}
}
return (
<ul $HasKeyedChildren className="TreeNode">
{children}
</ul>
);
}
TreeNode.defaultHooks = SCU_hooks;
function tree(data) {
/*
* $HasVNodeChildren flag is not needed here, because shape of children is known by the compiler
* <div> has static children <TreeNode> so there is no need for $HasVNodeChildren
*/
return (
<div className="Tree">
<TreeNode data={data.root} />
</div>
);
}
function AnimBox({ data }) {
var time = data.time % 10;
var style = 'border-radius:' + time + 'px;' + 'background:rgba(0,0,0,' + (0.5 + time / 10) + ')';
// We don't need to use $HasVNodeChildren here, because there is no Children
return <div data-id={data.id} style={style} className="AnimBox" />;
}
AnimBox.defaultHooks = SCU_hooks;
function anim(data) {
var items = data.items;
var length = items.length;
var children = [];
for (var i = 0; i < length; i++) {
var item = items[i];
children.push(<AnimBox data={item} key={item.id} />);
}
return (
<div $HasKeyedChildren className="Anim">
{children}
</div>
);
}
function onClick(text, e) {
console.log('Clicked', text);
e.stopPropagation();
}
function TableCell({ children }) {
/*
* Here we want to optimize for having text child vNode,
* It can be done by using $HasVNodeChildren on parent element
* and manually calling createTextVNode(value) for the children
*
* linkEvent is used here to bind the first parameter (text) into second parameter onClick function
* linkEvent has benefit of not creating function, it basically returns pre-defined object shape that inferno knows how to handle
* the main benefit is that no ".bind" or arrow function "() => {}" is needed. It works well with functional Components
*/
return (
<td $HasTextChildren onClick={linkEvent(children, onClick)} className="TableCell">
{children}
</td>
);
}
TableCell.defaultHooks = SCU_hooks;
function TableRow({ data }) {
var classes = 'TableRow';
if (data.active) {
classes = 'TableRow active';
}
var cells = data.props;
var length = cells.length + 1;
var children = [<TableCell>{'#' + data.id}</TableCell>];
for (var i = 1; i < length; i++) {
children.push(<TableCell>{cells[i - 1]}</TableCell>);
}
/*
* Again there is element vNode which children is always constant shape.
* Add optimization flags for Children type
* This time childrens does not have key, so the type is $HasNonKeyedChildren
*/
return (
<tr data-id={data.id} className={classes} $HasNonKeyedChildren>
{children}
</tr>
);
}
TableRow.defaultHooks = SCU_hooks;
function table(data) {
var items = data.items;
var length = items.length;
var children = [];
for (var i = 0; i < length; i++) {
var item = items[i];
// Components does not need ChildrenType flags, it does not hurt, but gains nothing
// Because Component children will be passed through props and will not be normalized before rendering anyway
children.push(<TableRow data={item} key={item.id} />);
}
/*
* When its known that given element has only one type of children we can optimize this compile time
* by adding children type $HasKeyedChildren (list of children - all keyed)
* $HasKeyedChildren means that there are no holes in the children array and all keys are correctly set
*/
return (
<table $HasKeyedChildren className="Table">
{children}
</table>
);
}
function main(data) {
var location = data.location;
var section;
if (location === 'table') {
section = table(data.table);
} else if (location === 'anim') {
section = anim(data.anim);
} else if (location === 'tree') {
section = tree(data.tree);
}
/*
* We know that this div will always have single vNode as its children,
* so we can optimize here and add flag $NoNormalize
*/
return (
<div $HasVNodeChildren className="Main">
{section}
</div>
);
}
document.addEventListener('DOMContentLoaded', function (e) {
var container = document.querySelector('#App');
uibench.run(
function (state) {
render(main(state), container);
},
function (samples) {
render(<pre $HasTextChildren>{JSON.stringify(samples, null, ' ')}</pre>, container);
}
);
});