Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

benchmark.js 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. 'use strict'
  2. var benchmark = require('benchmark')
  3. var suite = new benchmark.Suite()
  4. var fs = require('fs')
  5. var myData = fs.readFileSync('./package.json')
  6. var Parse = require('./')
  7. var safe = require('json-parse-safe')
  8. var max = 100
  9. suite.add('Parse class', function parseBench () {
  10. var parsed = new Parse(myData)
  11. if (parsed.err) {
  12. return
  13. }
  14. // add a loop to simulate some activity here
  15. for (var i = 0; i < max; i++) {
  16. var count = 0
  17. var keys = Object.keys(parsed.value)
  18. for (var k = 0; k < keys.length; k++) {
  19. count++
  20. }
  21. }
  22. })
  23. suite.add('Parse wrapped', function parseBench () {
  24. var parsed = Parse(myData)
  25. if (parsed.err) {
  26. return
  27. }
  28. // add a loop to simulate some activity here
  29. for (var i = 0; i < max; i++) {
  30. var count = 0
  31. var keys = Object.keys(parsed.value)
  32. for (var k = 0; k < keys.length; k++) {
  33. count++
  34. }
  35. }
  36. })
  37. suite.add('json-parse-safe', function parseBench () {
  38. var parsed = safe(myData)
  39. if (parsed.error) {
  40. return
  41. }
  42. // add a loop to simulate some activity here
  43. for (var i = 0; i < max; i++) {
  44. var count = 0
  45. var keys = Object.keys(parsed.value)
  46. for (var k = 0; k < keys.length; k++) {
  47. count++
  48. }
  49. }
  50. })
  51. suite.add('try catch here', function tryCatchBench () {
  52. var data = null
  53. try {
  54. data = JSON.parse(myData)
  55. } catch (err) {
  56. return
  57. }
  58. // add a loop to simulate some activity here
  59. for (var i = 0; i < max; i++) {
  60. var count = 0
  61. var keys = Object.keys(data)
  62. for (var k = 0; k < keys.length; k++) {
  63. count++
  64. }
  65. }
  66. })
  67. suite.on('complete', function print () {
  68. for (var i = 0; i < this.length; i++) {
  69. console.log(this[i].toString())
  70. }
  71. console.log('Fastest is', this.filter('fastest').map('name')[0])
  72. })
  73. suite.run()