1 | <!DOCTYPE html>
|
2 | <html lang="en">
|
3 |
|
4 | <head>
|
5 | <meta charset="UTF-8">
|
6 | <meta name="viewport" content="width=device-width, initial-scale=1.0">
|
7 | <meta http-equiv="X-UA-Compatible" content="ie=edge">
|
8 | <title>Radi.js - Large Data</title>
|
9 | </head>
|
10 |
|
11 | <body>
|
12 | <div id="app"></div>
|
13 | <script>
|
14 | const rand = Math.random
|
15 |
|
16 | function buildData(count, start) {
|
17 | start = (start) ? start : 0;
|
18 | const adjectives = [
|
19 | "pretty",
|
20 | "large",
|
21 | "big",
|
22 | "small",
|
23 | "tall",
|
24 | "short",
|
25 | "long",
|
26 | "handsome",
|
27 | "plain",
|
28 | "quaint",
|
29 | "clean",
|
30 | "elegant",
|
31 | "easy",
|
32 | "angry",
|
33 | "crazy",
|
34 | "helpful",
|
35 | "mushy",
|
36 | "odd",
|
37 | "unsightly",
|
38 | "adorable",
|
39 | "important",
|
40 | "inexpensive",
|
41 | "cheap",
|
42 | "expensive",
|
43 | "fancy",
|
44 | ]
|
45 |
|
46 | const colours = [
|
47 | "red",
|
48 | "yellow",
|
49 | "blue",
|
50 | "green",
|
51 | "pink",
|
52 | "brown",
|
53 | "purple",
|
54 | "brown",
|
55 | "white",
|
56 | "black",
|
57 | "orange",
|
58 | ]
|
59 |
|
60 | const nouns = [
|
61 | "table",
|
62 | "chair",
|
63 | "house",
|
64 | "bbq",
|
65 | "desk",
|
66 | "car",
|
67 | "pony",
|
68 | "cookie",
|
69 | "sandwich",
|
70 | "burger",
|
71 | "pizza",
|
72 | "mouse",
|
73 | "keyboard",
|
74 | ]
|
75 |
|
76 | var i = start + 1;
|
77 | return new Array(count).fill(0).map(_ => ({
|
78 | id: i++, value: `${adjectives[
|
79 | rand() * 1000 % adjectives.length >> 0]} ${colours[
|
80 | rand() * 1000 % colours.length >> 0]} ${nouns[
|
81 | rand() * 1000 % nouns.length >> 0]}`
|
82 | }))
|
83 | }
|
84 |
|
85 | var bnc = {};
|
86 | var av = (k = 'null', i) => {
|
87 | if (typeof bnc[k] === 'undefined') bnc[k] = [];
|
88 | var p = bnc[k];
|
89 | p.push(i);
|
90 | return p.reduce((a, b) => (a + b)) / p.length;
|
91 | }
|
92 | var bench = (k, c, cb) => {
|
93 | let t;
|
94 | c();
|
95 | t = performance.now();
|
96 | window.requestAnimationFrame(() => {
|
97 | let e = performance.now();
|
98 | console.log(k, (e - t).toFixed(2), ', AVG =', av(k, e - t).toFixed(2) + 'ms');
|
99 | cb((e - t).toFixed(2));
|
100 | });
|
101 | };
|
102 |
|
103 | function random_rgba() {
|
104 | var o = Math.round, r = Math.random, s = 175, i = 50;
|
105 | return 'rgb(' + o(r() * s + i) + ',' + o(r() * s + i) + ',' + o(r() * s + i) + ')';
|
106 | }
|
107 | </script>
|
108 | <script src="../../dist/radi.min.js"></script>
|
109 | <script src="../../src/devTools"></script>
|
110 | <script src="../../src/radi-http.js"></script>
|
111 | <script>
|
112 | var perf0 = performance.now()
|
113 | const { r, l, component, mount, cond, use, register } = radi
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 | http.get('/api', { results: 2 })
|
137 | .then((response) => {
|
138 | response.headers
|
139 | response.status
|
140 | response.text()
|
141 | response.json()
|
142 | var json = response.json()
|
143 | }, (err) => {
|
144 |
|
145 |
|
146 | });
|
147 |
|
148 | const state = {
|
149 | name: 'Marcis',
|
150 | num: 0,
|
151 | time: 0,
|
152 | count: 0,
|
153 | color: 'red',
|
154 | show: false,
|
155 | list: [],
|
156 | bench: '-'
|
157 | }
|
158 |
|
159 | const actions = {
|
160 | onMount() {
|
161 | console.log('Mounted in', performance.now() - perf0, 'ms');
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 | },
|
175 | toggle(events) {
|
176 | console.log(this, events);
|
177 | bench('Toggle element', () => {
|
178 | this.show = !this.show;
|
179 | }, (b) => {
|
180 | this.bench = b;
|
181 | });
|
182 | },
|
183 | reverse() {
|
184 | bench('Reverse list', () => {
|
185 | this.list.reverse();
|
186 | }, (b) => {
|
187 | this.bench = b;
|
188 | });
|
189 | },
|
190 | create1000() {
|
191 | bench('Create 10 rows', () => {
|
192 | this.list = buildData(10, this.list.length);
|
193 | this.count = 10;
|
194 | }, (b) => {
|
195 | this.bench = b;
|
196 | });
|
197 | },
|
198 | add1000() {
|
199 | bench('Add 1,000 rows', () => {
|
200 | this.list = this.list.concat(buildData(1000, this.list.length));
|
201 | this.count += 1000;
|
202 | }, (b) => {
|
203 | this.bench = b;
|
204 | });
|
205 | },
|
206 | add10000() {
|
207 | bench('Add 10,000 rows', () => {
|
208 | this.list = this.list.concat(buildData(10000, this.list.length));
|
209 | this.count += 10000;
|
210 | }, (b) => {
|
211 | this.bench = b;
|
212 | });
|
213 | },
|
214 | pop() {
|
215 | bench('Remove 1 row', () => {
|
216 | this.list.pop();
|
217 | this.count -= 1;
|
218 | }, (b) => {
|
219 | this.bench = b;
|
220 | });
|
221 | },
|
222 | update(events) {
|
223 | bench('Update every 10th row', () => {
|
224 | for (var i = 0; i < this.list.length; i++) {
|
225 | if (!((i + 1) % 10)) this.list[i] = { value: this.list[i].value + ' !!!' };
|
226 | }
|
227 | }, (b) => {
|
228 | this.bench = b;
|
229 | });
|
230 | },
|
231 | remove(events) {
|
232 | bench('Remove all rows', () => {
|
233 | this.list.splice(0, this.list.length);
|
234 | this.count = 0;
|
235 | }, (b) => {
|
236 | this.bench = b;
|
237 | });
|
238 | },
|
239 | swap(events) {
|
240 | bench('Swap 5th and 10th rows', () => {
|
241 | var x = 4, y = 9;
|
242 | this.list[x] = this.list.splice(y, 1, this.list[x])[0];
|
243 | }, (b) => {
|
244 | this.bench = b;
|
245 | });
|
246 | }
|
247 | }
|
248 |
|
249 | const reg = component({
|
250 | name: 'reg-comp',
|
251 | view: function () {
|
252 | return r('h1', 'This is ', l(this.name), ' component')
|
253 | },
|
254 | props: {
|
255 | name: String,
|
256 | }
|
257 | })
|
258 |
|
259 | register(reg)
|
260 |
|
261 | const view = function () {
|
262 | var name = l(this.name);
|
263 | return r('div',
|
264 | {
|
265 | style: {
|
266 | 'white-space': 'pre'
|
267 | }
|
268 | },
|
269 | r('reg-comp', { name: name }),
|
270 | '\n\n\n',
|
271 | r('img', { src: 'https://img.strike.lv/i/avatars/f3/94/5557907d62b2fc97549394f3.png' }),
|
272 | r('h4',
|
273 | '[dynamic predefined] My name is ',
|
274 | name
|
275 | ),
|
276 | r('h4',
|
277 | '[dynamic] My name is ',
|
278 | l(this.name + ' Bergmanis')
|
279 | ),
|
280 | r('h4',
|
281 | '[static] My name is ',
|
282 | this.name
|
283 | ),
|
284 | r('input', { type: 'email', autofocus: true, model: l(this.name) }),
|
285 | r('hr'),
|
286 | cond(
|
287 | l(this.show),
|
288 | r('div',
|
289 | {
|
290 | style: {
|
291 | 'color': l(this.color)
|
292 | }
|
293 | },
|
294 | '\nThis refreshes 60fps:\n',
|
295 | l(this.num),
|
296 | '\n\n',
|
297 | 'This refreshes every second:\n',
|
298 | l(this.time)
|
299 | )
|
300 | ).else(
|
301 | r('div', '[ There is something hidden here ]')
|
302 | ),
|
303 | r('button',
|
304 | { onclick: () => this.toggle('asd') },
|
305 | 'Toggle Color Test'
|
306 | ),
|
307 | r('hr'),
|
308 | r('div',
|
309 | r('h3',
|
310 | 'Item count: ',
|
311 | l(this.count),
|
312 | ),
|
313 | r('h3',
|
314 | cond(
|
315 | l(this.count < 1000),
|
316 | '< 1000'
|
317 | ).cond(
|
318 | l(this.count < 2000),
|
319 | '< 2000'
|
320 | ).cond(
|
321 | l(this.count < 3000),
|
322 | '< 3000'
|
323 | ).cond(
|
324 | l(this.count < 4000),
|
325 | '< 4000'
|
326 | ).else(
|
327 | '>= 4000'
|
328 | )
|
329 | ),
|
330 | 'Benchmark: ',
|
331 | l(this.bench),
|
332 | ' ms',
|
333 | r('div',
|
334 | r('button',
|
335 | { onclick: this.create1000 },
|
336 | 'Create 10 rows'
|
337 | ),
|
338 | r('button',
|
339 | { onclick: this.add1000 },
|
340 | 'Add 1,000 rows'
|
341 | ),
|
342 | r('button',
|
343 | { onclick: this.add10000 },
|
344 | 'Add 10,000 rows'
|
345 | ),
|
346 | '\n',
|
347 | r('button',
|
348 | { onclick: this.pop },
|
349 | 'Remove 1 row'
|
350 | ),
|
351 | r('button',
|
352 | { onclick: this.reverse },
|
353 | 'Reverse Test'
|
354 | ),
|
355 | r('button',
|
356 | { onclick: this.swap },
|
357 | 'Swap Test'
|
358 | ),
|
359 | r('button',
|
360 | { onclick: this.remove },
|
361 | 'Remove all'
|
362 | )
|
363 | ),
|
364 | r('ul',
|
365 | 'Child node on top\n\n',
|
366 |
|
367 | list(l(this.list), (item) => r('li',
|
368 | l(item.id),
|
369 | ' - ',
|
370 | l(item.value)
|
371 | )),
|
372 | '\nChild node at bottom'
|
373 | )
|
374 | )
|
375 | );
|
376 | };
|
377 |
|
378 | const main = component({
|
379 | view: view,
|
380 | state: state,
|
381 | actions: actions
|
382 | });
|
383 |
|
384 | mount(r('div', new main()), 'app');
|
385 | </script>
|
386 | </body>
|
387 |
|
388 | </html>
|