Guest User

Untitled

a guest
Jun 18th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. const { Suite } = require('benchmark');
  2. const cursor = require('ansi')(process.stdout);
  3.  
  4. const cycle = (e, newline) => {
  5. cursor.eraseLine();
  6. cursor.horizontalAbsolute();
  7. cursor.write('' + e.target);
  8. if (newline) cursor.write('\n');
  9. };
  10.  
  11. function bench(name) {
  12. const suite = new Suite()
  13. .on('start', () => console.log(`# ${name}`))
  14. .on('complete', function() {
  15. const fastest = this.filter('fastest').map('name').toString();
  16. console.log(`Fastest is '${fastest}'`);
  17. console.log();
  18. });
  19.  
  20. const ste = {
  21. run: suite.run.bind(suite),
  22. add(key, fn) {
  23. suite.add(key, {
  24. onCycle: e => cycle(e),
  25. onComplete: e => cycle(e, true),
  26. onError(err) {
  27. console.error(err);
  28. process.exit(1);
  29. },
  30. fn
  31. });
  32. return ste;
  33. }
  34. };
  35. return ste;
  36. }
  37.  
  38. /**
  39. * Example usage
  40. */
  41.  
  42. bench('some-comparisons')
  43. .add('one', () => {
  44. // do stuff
  45. })
  46. .add('two', () => {
  47. // do stuff
  48. })
  49. .add('three', () => {
  50. // do stuff
  51. })
  52. .run()
Add Comment
Please, Sign In to add comment