access log date format

JavaScript performance comparison

Test case created by fengmk2

Info

Preparation code

<script src="http://nfs.nodeblog.org/9/8/983e0324fd26b84347be65027528240b.js"></script>

<script>
var fasterAccessDate = function () {
  // 0   1   2  3    4        5        6
  // Tue Apr 16 2013 16:40:09 GMT+0800 (CST)
  // =>
  // 16/Apr/2013:16:40:09 +0800
  var dates = Date().split(' ');
  return dates[2] + '/' + dates[1] + '/' + dates[3] + ':' + dates[4] + ' ' + dates[5].substring(3);
};

var fasterAccessDate2 = function () {
  // 0   1   2  3    4        5        6
  // Tue Apr 16 2013 16:40:09 GMT+0800 (CST)
  // =>
  // 16/Apr/2013:16:40:09 +0800
  var dates = new Date().toString().split(' ');
  return dates[2] + '/' + dates[1] + '/' + dates[3] + ':' + dates[4] + ' ' + dates[5].substring(3);
};

var MONTHS = [
  'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
];
// only set once.
var TIMEZONE = ' ';
var _hourOffset = parseInt(-(new Date().getTimezoneOffset()) / 60, 10);
if (_hourOffset > 0) {
  TIMEZONE += '+';
} else {
  TIMEZONE += '-';
}
_hourOffset = Math.abs(_hourOffset);
if (_hourOffset < 10) {
  _hourOffset = '0' + _hourOffset;
}
TIMEZONE += _hourOffset + '00';

var accessLogDate = function () {
  // 16/Apr/2013:16:40:09 +0800
  var d = new Date();
  var date = d.getDate();
  if (date < 10) {
    date = '0' + date;
  }
  var hours = d.getHours();
  if (hours < 10) {
    hours = '0' + hours;
  }
  var mintues = d.getMinutes();
  if (mintues < 10) {
    mintues = '0' + mintues;
  }
  var seconds = d.getSeconds();
  if (seconds < 10) {
    seconds = '0' + seconds;
  }
  return date + '/' + MONTHS[d.getMonth()] + '/' + d.getFullYear() +
    ':' + hours + ':' + mintues + ':' + seconds + TIMEZONE;
};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
moment().format('DD/MMM/YYYY:HH:mm:ss ZZ')
moment().format('DD/MMM/YYYY:HH:mm:ss ZZ');
pending…
new Date().toString()
new Date().toString();
pending…
Date()
Date();
pending…
fasterAccessDate()
fasterAccessDate();
pending…
fasterAccessDate2()
fasterAccessDate2();
pending…
accessLogDate()
accessLogDate();
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment