instantiation.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. console.log('\nInstantiation\n')
  2. var done = null
  3. var OldVue = require('../../vue')
  4. var Vue = require('../src/vue')
  5. var sideEffect = null
  6. var parent = new Vue({
  7. data: { a: 1 }
  8. })
  9. var oldParent = new OldVue({
  10. data: { a: 1 }
  11. })
  12. function now () {
  13. return window.performence
  14. ? window.performence.now()
  15. : Date.now()
  16. }
  17. // warm up
  18. for (var i = 0; i < 1000; i++) {
  19. sideEffect = new Vue()
  20. }
  21. var queue = []
  22. function bench (desc, n, fn) {
  23. queue.push(function () {
  24. var s = now()
  25. for (var i = 0; i < n; i++) {
  26. fn()
  27. }
  28. var time = now() - s
  29. var opf = (16 / (time / n)).toFixed(2)
  30. console.log(desc + ' ' + n + ' times - ' + opf + ' ops/frame')
  31. })
  32. }
  33. function run () {
  34. queue.shift()()
  35. if (queue.length) {
  36. setTimeout(run, 0)
  37. } else {
  38. done && done()
  39. }
  40. }
  41. function simpleInstance () {
  42. sideEffect = new Vue({
  43. el: document.createElement('div'),
  44. data: {a: 1}
  45. })
  46. }
  47. function oldSimpleInstance () {
  48. sideEffect = new OldVue({
  49. data: {a: 1}
  50. })
  51. }
  52. function simpleInstanceWithInheritance () {
  53. sideEffect = new Vue({
  54. el: document.createElement('div'),
  55. parent: parent,
  56. data: { b:2 }
  57. })
  58. }
  59. function oldSimpleInstanceWithInheritance () {
  60. sideEffect = new OldVue({
  61. parent: oldParent,
  62. data: { b:2 }
  63. })
  64. }
  65. function complexInstance () {
  66. sideEffect = new Vue({
  67. el: document.createElement('div'),
  68. data: {
  69. a: {
  70. b: {
  71. c: 1
  72. }
  73. },
  74. c: {
  75. b: {
  76. c: { a:1 },
  77. d: 2,
  78. e: 3,
  79. d: 4
  80. }
  81. },
  82. e: [{a:1}, {a:2}, {a:3}]
  83. }
  84. })
  85. }
  86. function oldComplexInstance () {
  87. sideEffect = new OldVue({
  88. data: {
  89. a: {
  90. b: {
  91. c: 1
  92. }
  93. },
  94. c: {
  95. b: {
  96. c: { a:1 },
  97. d: 2,
  98. e: 3,
  99. d: 4
  100. }
  101. },
  102. e: [{a:1}, {a:2}, {a:3}]
  103. }
  104. })
  105. }
  106. bench('Simple instance', 10, simpleInstance)
  107. bench('Simple instance', 100, simpleInstance)
  108. bench('Simple instance', 1000, simpleInstance)
  109. bench('Simple instance (old)', 10, oldSimpleInstance)
  110. bench('Simple instance (old)', 100, oldSimpleInstance)
  111. bench('Simple instance (old)', 1000, oldSimpleInstance)
  112. bench('Simple instance with inheritance', 10, simpleInstanceWithInheritance)
  113. bench('Simple instance with inheritance', 100, simpleInstanceWithInheritance)
  114. bench('Simple instance with inheritance', 1000, simpleInstanceWithInheritance)
  115. bench('Simple instance with inheritance (old)', 10, oldSimpleInstanceWithInheritance)
  116. bench('Simple instance with inheritance (old)', 100, oldSimpleInstanceWithInheritance)
  117. bench('Simple instance with inheritance (old)', 1000, oldSimpleInstanceWithInheritance)
  118. bench('Complex instance', 10, complexInstance)
  119. bench('Complex instance', 100, complexInstance)
  120. bench('Complex instance', 1000, complexInstance)
  121. bench('Complex instance (old)', 10, oldComplexInstance)
  122. bench('Complex instance (old)', 100, oldComplexInstance)
  123. bench('Complex instance (old)', 1000, oldComplexInstance)
  124. exports.run = function (cb) {
  125. done = cb
  126. run()
  127. }