toLocaleTimeString

Benchmark created on


Description

Comparing different ways of getting friendly time strings out of a Date object.

Setup

var times = [];
    
    for (var i = 0; i < 1000; i++) {
      times.push(new Date(Date.now()));
    }
    
    var options = {
      hour: '2-digit',
      minute: '2-digit'
    };
    
    var lang = navigator.language;

Teardown


    times = [];
  

Test runner

Ready to run.

Testing in
TestOps/sec
toLocaleTimeString no options
for (var i = 0; i < times.length; i++) {
  var ts = times[i].toLocaleTimeString();
}
ready
toLocaleTimeString options
for (var i = 0; i < times.length; i++) {
  var ts = times[i].toLocaleTimeString(navigator.language, {
    hour: '2-digit',
    minute: '2-digit'
  });
}
ready
string concatination
for (var i = 0; i < times.length; i++) {
  var d = times[i];
  var m = d.getMinutes();
  var s = d.getSeconds();
  var h = d.getHours();
  var ampm = (h >= 12) ? "PM" : "AM";

  var ts = h + ':' + m + ' ' + ampm;
}
ready
toLocaleTimeString options saved
for (var i = 0; i < times.length; i++) {
  var ts = times[i].toLocaleTimeString(lang, options);
}
ready
Intl.DateTimeFormat
var formatter = new Intl.DateTimeFormat(navigator.language, {
  hour: 'numeric',
  minute: 'numeric',
  second: 'numeric',
  timeZoneName: 'short'
});

for (var i = 0; i < times.length; i++) {
  var ts = formatter.format(times[i]);
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.