index.html 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Vue benchmark</title>
  6. </head>
  7. <body>
  8. <script src="https://cdn.jsdelivr.net/lodash/4.10.0/lodash.min.js"></script>
  9. <script src="../../dist/vue.min.js"></script>
  10. <style>
  11. .danger {
  12. background-color: red;
  13. }
  14. </style>
  15. <script type="text/x-template" id="t">
  16. <div>
  17. <h1>{{ items.length }} Components</h1>
  18. <p>{{ action }} took {{time}}ms.</p>
  19. <button @click="shuffle">shuffle</button>
  20. <button @click="add">add</button>
  21. <table class="table table-hover table-striped test-data">
  22. <row v-for="item in items" :key="item.id"
  23. :class="{ danger: item.id === selected }"
  24. :item="item"
  25. @select="select(item)"
  26. @remove="remove(item)">
  27. </row>
  28. </table>
  29. </div>
  30. </script>
  31. <script type="text/x-template" id="row">
  32. <tr>
  33. <td class="col-md-1">{{item.id}}</td>
  34. <td class="col-md-4">
  35. <a @click="$emit('select')">{{item.label}}</a>
  36. </td>
  37. <td class="col-md-1">
  38. <button @click="$emit('remove')">remove</button>
  39. </td>
  40. </tr>
  41. </script>
  42. <div id="el">
  43. </div>
  44. <script>
  45. var total = 1000
  46. var items = []
  47. for (var i = 0; i < total; i++) {
  48. items.push({
  49. id: i,
  50. label: String(Math.random()).slice(0, 5)
  51. })
  52. }
  53. var s = window.performance.now()
  54. console.profile('render')
  55. var vm = new Vue({
  56. el: '#el',
  57. template: '#t',
  58. data: {
  59. total: total,
  60. time: 0,
  61. action: 'Render',
  62. items: items,
  63. selected: null
  64. },
  65. methods: {
  66. shuffle: monitor('shuffle', function () {
  67. this.items = _.shuffle(this.items)
  68. }),
  69. add: monitor('add', function () {
  70. this.items.push({
  71. id: total++,
  72. label: String(Math.random()).slice(0, 5)
  73. })
  74. }),
  75. select: monitor('select', function (item) {
  76. this.selected = item.id
  77. }),
  78. remove: monitor('remove', function (item) {
  79. this.items.splice(this.items.indexOf(item), 1)
  80. })
  81. },
  82. components: {
  83. row: {
  84. props: ['item'],
  85. template: '#row'
  86. }
  87. }
  88. })
  89. setTimeout(function () {
  90. vm.time = window.performance.now() - s
  91. console.profileEnd('render')
  92. }, 0)
  93. function monitor (action, fn) {
  94. return function () {
  95. var s = window.performance.now()
  96. fn.apply(this, arguments)
  97. Vue.nextTick(function () {
  98. vm.action = action
  99. vm.time = window.performance.now() - s
  100. })
  101. }
  102. }
  103. </script>
  104. </body>
  105. </html>