instantiation.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. console.log('\nInstantiation\n')
  2. var Vue = require('../src/vue')
  3. var sideEffect = null
  4. var parent = new Vue({
  5. data: { a: 1 }
  6. })
  7. function getNano () {
  8. var hr = process.hrtime()
  9. return hr[0] * 1e9 + hr[1]
  10. }
  11. function now () {
  12. return process.hrtime
  13. ? getNano() / 1e6
  14. : window.performence
  15. ? window.performence.now()
  16. : Date.now()
  17. }
  18. // warm up
  19. for (var i = 0; i < 1000; i++) {
  20. sideEffect = new Vue()
  21. }
  22. function bench (desc, n, fn) {
  23. var s = now()
  24. for (var i = 0; i < n; i++) {
  25. fn()
  26. }
  27. var time = now() - s
  28. var opf = (16 / (time / n)).toFixed(2)
  29. console.log(desc + ' ' + n + ' times - ' + opf + ' ops/frame')
  30. }
  31. function simpleInstance () {
  32. sideEffect = new Vue({
  33. data: {a: 1}
  34. })
  35. }
  36. function simpleInstanceWithInheritance () {
  37. sideEffect = new Vue({
  38. parent: parent,
  39. data: { b:2 }
  40. })
  41. }
  42. function complexInstance () {
  43. sideEffect = new Vue({
  44. data: {
  45. a: {
  46. b: {
  47. c: 1
  48. }
  49. },
  50. c: {
  51. b: {
  52. c: { a:1 },
  53. d: 2,
  54. e: 3,
  55. d: 4
  56. }
  57. },
  58. e: [{a:1}, {a:2}, {a:3}]
  59. }
  60. })
  61. }
  62. bench('Simple instance', 10, simpleInstance)
  63. bench('Simple instance', 100, simpleInstance)
  64. bench('Simple instance', 1000, simpleInstance)
  65. bench('Simple instance with inheritance', 10, simpleInstanceWithInheritance)
  66. bench('Simple instance with inheritance', 100, simpleInstanceWithInheritance)
  67. bench('Simple instance with inheritance', 1000, simpleInstanceWithInheritance)
  68. bench('Complex instance', 10, complexInstance)
  69. bench('Complex instance', 100, complexInstance)
  70. bench('Complex instance', 1000, complexInstance)