date_format.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*!
  2. * utility - benchmark/date_format.js
  3. * Copyright(c) 2013 fengmk2 <fengmk2@gmail.com>
  4. * MIT Licensed
  5. */
  6. "use strict";
  7. /**
  8. * Module dependencies.
  9. */
  10. // http://jsperf.com/access-log-date-format
  11. var utils = require('../');
  12. var moment = require('moment');
  13. var Benchmark = require('benchmark');
  14. var suite = new Benchmark.Suite();
  15. var fasterAccessDate = function () {
  16. // 0 1 2 3 4 5 6
  17. // Tue Apr 16 2013 16:40:09 GMT+0800 (CST)
  18. // =>
  19. // 16/Apr/2013:16:40:09 +0800
  20. var dates = new Date().toString().split(' ');
  21. return dates[2] + '/' + dates[1] + '/' + dates[3] + ':' + dates[4] + ' ' + dates[5].substring(3);
  22. };
  23. var fasterAccessDate2 = function () {
  24. // 0 1 2 3 4 5 6
  25. // Tue Apr 16 2013 16:40:09 GMT+0800 (CST)
  26. // =>
  27. // 16/Apr/2013:16:40:09 +0800
  28. var dates = Date().split(' ');
  29. return dates[2] + '/' + dates[1] + '/' + dates[3] + ':' + dates[4] + ' ' + dates[5].substring(3);
  30. };
  31. console.log('moment().format("DD/MMM/YYYY:HH:mm:ss ZZ"): %j', moment().format('DD/MMM/YYYY:HH:mm:ss ZZ'));
  32. console.log('utils.accessLogDate(): %j', utils.accessLogDate());
  33. console.log('utils.logDate(): %j', utils.logDate());
  34. console.log('fasterAccessDate(): %j', fasterAccessDate());
  35. console.log('fasterAccessDate2(): %j', fasterAccessDate2());
  36. console.log('new Date().toString(): %j', new Date().toString());
  37. console.log('Date(): %j', Date());
  38. console.log('Date.now(): %j', Date.now());
  39. console.log('------------------------');
  40. suite
  41. .add("moment().format('DD/MMM/YYYY:HH:mm:ss ZZ')", function () {
  42. moment().format('DD/MMM/YYYY:HH:mm:ss ZZ');
  43. })
  44. .add('utils.accessLogDate()', function () {
  45. utils.accessLogDate();
  46. })
  47. .add('utils.logDate()', function () {
  48. utils.logDate();
  49. })
  50. .add('fasterAccessDate()', function () {
  51. fasterAccessDate();
  52. })
  53. .add('fasterAccessDate2()', function () {
  54. fasterAccessDate2();
  55. })
  56. .add('new Date().toString()', function () {
  57. new Date().toString();
  58. })
  59. .add('Date()', function () {
  60. Date();
  61. })
  62. .add('Date.now()', function () {
  63. Date.now();
  64. })
  65. // add listeners
  66. .on('cycle', function (event) {
  67. console.log(String(event.target));
  68. })
  69. .on('complete', function () {
  70. console.log('Fastest is ' + this.filter('fastest').pluck('name'));
  71. })
  72. .run({ async: false });