perf.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict';
  2. var oN = require('./O(n)');
  3. var es6Repeat = require('./es6Repeat');
  4. var current = require('../');
  5. var Benchmark = require('benchmark');
  6. var str = "abcd"
  7. var len = 100;
  8. function buildSuite (note, fns, args) {
  9. console.log(note);
  10. var suite = new Benchmark.Suite;
  11. Object.keys(fns).forEach(function (name) {
  12. suite.add(name, function () {
  13. fns[name].apply(null, args);
  14. });
  15. });
  16. suite.on('cycle', function (event) {
  17. console.log(String(event.target));
  18. }).on('complete', function () {
  19. console.log('Fastest is ' + this.filter('fastest').map('name'));
  20. });
  21. return suite;
  22. }
  23. var fns = {
  24. 'O(n)': oN,
  25. 'ES6 Repeat': es6Repeat,
  26. 'Current': current
  27. };
  28. buildSuite('-> pad 100 spaces to str of len 4', fns, ['abcd', 104, ' ']).run();
  29. buildSuite('-> pad 10 spaces to str of len 4', fns, ['abcd', 14, ' ']).run();
  30. buildSuite('-> pad 9 spaces to str of len 4', fns, ['abcd', 13, ' ']).run();
  31. buildSuite('-> pad 100 to str of len 100', fns, ['0012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789123456789', 200, ' ']).run();
  32. buildSuite('-> pad 10 to str of len 100', fns, ['0012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789123456789', 110, ' ']).run();
  33. buildSuite('-> pad 9 to str of len 100', fns, ['0012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789123456789', 109, ' ']).run();