1
0

loop.html 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>Alpine-3-keyed</title>
  6. <link href="/css/currentStyle.css" rel="stylesheet" />
  7. <script src="http://alpine-next.test/packages/alpinejs/dist/cdn.js" defer></script>
  8. <!-- <script src="https://cdn.jsdelivr.net/gh/alpinejs/alpine@v2.x.x/dist/alpine.min.js" defer></script> -->
  9. </head>
  10. <body>
  11. <div class="container" id="main" x-data="{ data: []}">
  12. <div class="jumbotron">
  13. <div class="row">
  14. <div class="col-md-6">
  15. <h1>Alpine-3-keyed</h1>
  16. </div>
  17. <div class="col-md-6">
  18. <div class="row">
  19. <div class="col-sm-6 smallpad">
  20. <button
  21. type="button"
  22. class="btn btn-primary btn-block"
  23. id="run"
  24. @click="run"
  25. >
  26. Create 1,000 rows
  27. </button>
  28. </div>
  29. <div class="col-sm-6 smallpad">
  30. <button
  31. type="button"
  32. class="btn btn-primary btn-block"
  33. id="runlots"
  34. @click="runLots"
  35. >
  36. Create 10,000 rows
  37. </button>
  38. </div>
  39. <div class="col-sm-6 smallpad">
  40. <button
  41. type="button"
  42. class="btn btn-primary btn-block"
  43. id="add"
  44. @click="add"
  45. >
  46. Append 1,000 rows
  47. </button>
  48. </div>
  49. <div class="col-sm-6 smallpad">
  50. <button
  51. type="button"
  52. class="btn btn-primary btn-block"
  53. id="update"
  54. @click="update"
  55. >
  56. Update every 10th row
  57. </button>
  58. </div>
  59. <div class="col-sm-6 smallpad">
  60. <button
  61. type="button"
  62. class="btn btn-primary btn-block"
  63. id="clear"
  64. @click="clear"
  65. >
  66. Clear
  67. </button>
  68. </div>
  69. <div class="col-sm-6 smallpad">
  70. <button
  71. type="button"
  72. class="btn btn-primary btn-block"
  73. id="swaprows"
  74. @click="swapRows"
  75. >
  76. Swap Rows
  77. </button>
  78. </div>
  79. </div>
  80. </div>
  81. </div>
  82. </div>
  83. <table class="table table-hover table-striped test-data">
  84. <tbody>
  85. <template x-for="item in data" :key="item.id">
  86. <tr :class="item.id === selected ? 'danger' : ''">
  87. <td class="col-md-1" x-text="item.id"></td>
  88. <td class="col-md-4">
  89. <a @click="select(item.id)" x-text="item.label"></a>
  90. </td>
  91. <td class="col-md-1">
  92. <a @click="remove(item.id)" >x</a>
  93. </td>
  94. <td class="col-md-6"></td>
  95. </tr>
  96. </template>
  97. </tbody>
  98. </table>
  99. <span
  100. class="preloadicon glyphicon glyphicon-remove"
  101. aria-hidden="true"
  102. ></span>
  103. </div>
  104. <script>
  105. let idCounter = 1;
  106. const adjectives = ["pretty", "large", "big", "small", "tall", "short", "long", "handsome", "plain", "quaint", "clean", "elegant", "easy", "angry", "crazy", "helpful", "mushy", "odd", "unsightly", "adorable", "important", "inexpensive", "cheap", "expensive", "fancy"],
  107. colours = ["red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black", "orange"],
  108. nouns = ["table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger", "pizza", "mouse", "keyboard"];
  109. function _random (max) { return Math.round(Math.random() * 1000) % max; };
  110. function buildData(count) {
  111. let data = new Array(count);
  112. for (let i = 0; i < count; i++) {
  113. data[i] = {
  114. id: idCounter++,
  115. label: `${adjectives[_random(adjectives.length)]} ${colours[_random(colours.length)]} ${nouns[_random(nouns.length)]}`
  116. }
  117. }
  118. return data;
  119. }
  120. function app() {
  121. return {
  122. data: [],
  123. selected: undefined,
  124. add() {
  125. let start = performance.now()
  126. this.data = this.data.concat(buildData(1000))
  127. setTimeout(() => {
  128. console.log(performance.now() - start);
  129. }, 0)
  130. },
  131. clear() {
  132. let start = performance.now()
  133. this.data = [];
  134. this.selected = undefined;
  135. setTimeout(() => {
  136. console.log(performance.now() - start);
  137. }, 0)
  138. },
  139. update() {
  140. let start = performance.now()
  141. for (let i = 0; i < this.data.length; i += 10) {
  142. this.data[i].label += ' !!!';
  143. }
  144. setTimeout(() => {
  145. console.log(performance.now() - start);
  146. }, 0)
  147. },
  148. remove(id) {
  149. let start = performance.now()
  150. const idx = this.data.findIndex(d => d.id === id);
  151. this.data.splice(idx, 1);
  152. setTimeout(() => {
  153. console.log(performance.now() - start);
  154. }, 0)
  155. },
  156. run() {
  157. let start = performance.now()
  158. this.data = buildData(100);
  159. this.selected = undefined;
  160. setTimeout(() => {
  161. console.log(performance.now() - start);
  162. }, 0)
  163. },
  164. runLots() {
  165. let start = performance.now()
  166. this.data = buildData(10000);
  167. this.selected = undefined;
  168. setTimeout(() => {
  169. console.log(performance.now() - start);
  170. }, 0)
  171. },
  172. select(id) {
  173. let start = performance.now()
  174. this.selected = id
  175. setTimeout(() => {
  176. console.log(performance.now() - start);
  177. }, 0)
  178. },
  179. swapRows() {
  180. let start = performance.now()
  181. const d = this.data;
  182. if (d.length > 998) {
  183. const tmp = d[998];
  184. d[998] = d[1];
  185. d[1] = tmp;
  186. }
  187. setTimeout(() => {
  188. console.log(performance.now() - start);
  189. }, 0)
  190. }
  191. }
  192. }
  193. </script>
  194. </body>
  195. </html>