index.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. var bench = require('nanobench')
  2. var src = require('fs').readFileSync(require.resolve('acorn'))
  3. var parse = require('acorn').parse
  4. var astw = require('astw')
  5. var eswalk = require('estree-walk')
  6. var eswalker = require('estree-walker')
  7. var dash = require('../')
  8. bench('astw', function (b) {
  9. var ast = parse(src)
  10. b.start()
  11. var i = 0
  12. astw(ast)(function (node) { i++ })
  13. b.end('walked ' + i + ' nodes')
  14. })
  15. bench('estree-walk', function (b) {
  16. var ast = parse(src)
  17. b.start()
  18. var i = 0
  19. eswalk(ast, function (node) { i++ })
  20. b.end('walked ' + i + ' nodes')
  21. })
  22. bench('estree-walk steps', function (b) {
  23. var ast = parse(src)
  24. b.start()
  25. var i = 0
  26. for (var q = [ast], node; (node = q.pop()); eswalk.step(node, q)) {
  27. i++
  28. }
  29. b.end('walked ' + i + ' nodes')
  30. })
  31. bench('dash-ast', function (b) {
  32. var ast = parse(src)
  33. b.start()
  34. var i = 0
  35. dash(ast, function (node) { i++ })
  36. b.end('walked ' + i + ' nodes')
  37. })
  38. bench('dash-ast with .parent', function (b) {
  39. var ast = parse(src)
  40. b.start()
  41. var i = 0
  42. dash.withParent(ast, function (node) { i++ })
  43. b.end('walked ' + i + ' nodes')
  44. })
  45. bench('dash-ast with enter/leave', function (b) {
  46. var ast = parse(src)
  47. b.start()
  48. var i = 0
  49. var j = 0
  50. dash(ast, {
  51. enter: function (node) { i++ },
  52. leave: function (node) { j++ }
  53. })
  54. b.end('walked ' + [i, j] + ' nodes')
  55. })
  56. bench('estree-walker', function (b) {
  57. var ast = parse(src)
  58. b.start()
  59. var i = 0
  60. eswalker.walk(ast, {
  61. enter: function (node) { i++ }
  62. })
  63. b.end('walked ' + i + ' nodes')
  64. })