Last active
December 21, 2015 12:02
-
-
Save robertknight/4b697a7097f8317d98f1 to your computer and use it in GitHub Desktop.
Date formatting benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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