1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ < head >
4+ < meta charset ="UTF-8 ">
5+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
6+ < meta http-equiv ="X-UA-Compatible " content ="ie=edge ">
7+ < script defer src ="https://unpkg.com/hyperhtml@latest/min.js "> </ script >
8+ < script >
9+ this . onnload = ( ) => {
10+ const numberOfElements = 10000 ;
11+ const items = Array . apply ( null , Array ( numberOfElements ) ) . map ( ( el , i ) => ( { text : i } ) ) . sort ( ( ) => .5 < Math . random ( ) )
12+ const sortMethods = [
13+ ( ) => 0 ,
14+ ( a , b ) => a . text - b . text ,
15+ ( a , b ) => b . text - a . text
16+ ] ;
17+
18+ function hyperHtmlTest ( ) {
19+
20+ const $node = document . createElement ( 'div' ) ;
21+ const $div = document . createElement ( 'div' ) ;
22+ const $button = document . createElement ( 'button' ) ;
23+
24+ const bound = hyperHTML . bind ( $div ) ;
25+ const wire = hyperHTML . wire ;
26+
27+ let sortMethodIndex = 0 ;
28+
29+ function render ( ) {
30+
31+ const sortMethod = sortMethods [ sortMethodIndex ++ % sortMethods . length ] ;
32+ bound `${ items . concat ( ) . sort ( sortMethod ) . map (
33+ item => wire ( item ) `<p>${ item . text } </p>`
34+ ) } `;
35+ }
36+
37+ $node . appendChild ( $button ) ;
38+ $node . appendChild ( $div ) ;
39+
40+ $button . textContent = 'HyperHTml Sort' ;
41+ $button . onclick = render ;
42+
43+ return $node ;
44+
45+ }
46+
47+ function normalHtmlTest ( ) {
48+
49+ const $node = document . createElement ( 'div' )
50+ const $div = document . createElement ( 'div' )
51+ const $button = document . createElement ( 'button' )
52+
53+ let sortMethodIndex = 0
54+
55+ function render ( ) {
56+
57+ const sortMethod = sortMethods [ sortMethodIndex ++ % sortMethods . length ] ;
58+
59+ while ( $div . childNodes . length )
60+ $div . removeChild ( $div . childNodes [ 0 ] ) ;
61+
62+ const frag = document . createDocumentFragment ( ) ;
63+
64+ items . concat ( ) . sort ( sortMethod ) . forEach ( item => {
65+
66+ const p = document . createElement ( 'p' ) ;
67+
68+ p . textContent = item . text ;
69+
70+ frag . appendChild ( p ) ;
71+
72+ } )
73+
74+ $div . appendChild ( frag ) ;
75+
76+ }
77+
78+ $node . appendChild ( $button ) ;
79+ $node . appendChild ( $div ) ;
80+
81+ $button . textContent = 'NormalHtml Sort' ;
82+ $button . onclick = render ;
83+
84+ return $node ;
85+
86+ }
87+
88+ document . body . appendChild ( normalHtmlTest ( ) ) ;
89+ document . body . appendChild ( hyperHtmlTest ( ) ) ;
90+
91+ } ;
92+ </ script >
93+ </ head >
94+ < body >
95+
96+ </ body >
97+ </ html >
0 commit comments