Skip to content

Instantly share code, notes, and snippets.

@robertknight
Last active December 21, 2015 12:02
Show Gist options
  • Select an option

  • Save robertknight/4b697a7097f8317d98f1 to your computer and use it in GitHub Desktop.

Select an option

Save robertknight/4b697a7097f8317d98f1 to your computer and use it in GitHub Desktop.
Date formatting benchmark
var Benchmark = require('benchmark');
var moment = require('moment');
var formatOpts = {
year: 'numeric',
month: 'short',
day: '2-digit',
weekday: 'long',
hour: '2-digit',
minute: '2-digit',
};
var dateStr = (new Date).toISOString();
var formatter = new Intl.DateTimeFormat(undefined, formatOpts);
var suite = new Benchmark.Suite;
suite.add('toLocaleDateString without options', function() {
var date = new Date(dateStr);
date.toLocaleDateString();
})
.add('toLocaleDateString with options', function() {
var date = new Date(dateStr);
date.toLocaleDateString(undefined, formatOpts);
})
.add('Intl.DateTimeFormat reuse', function() {
var date = new Date(dateStr);
formatter.format(date);
})
.add('Moment.js', function() {
moment(dateStr).format('LLLL');
})
.on('complete', function() {
this.forEach(function (bench) {
console.log('%s: %d iterations', bench.name, bench.count);
});
})
.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment