Format your dates really really fast
Speed-date is a super-fast date formatter optimized for formatting lots of dates with the same format.
npm install speed-date
var speedDate = ;var theDate = 2006 1 3 12 34 56;var formatter = ;; // '2006-02-03 12:34:56'// alternatively:; // '2006-02-03 12:34:56'// alternatively alternatively:// faster when repeated lots with the same format,// useful as an alternative to keeping track of formatter as abovespeedDate; // '2006-02-03 12:34:56'
When called with just
format, generates a formatter function that takes date objects.
When called with both
date, creates a formatter function and applies it with the given date.
Behaves the same as
speedDate, but the generated formatter function is stored and reused if the same
format is used again. Only use this if you know you're only going to be using a few different possible formats (they're never cleaned up internally).
Exactly the same as above, but always interprets the date as UTC. Obviousy, in this mode,
ZZ tokens will always be
+0000 respectively. Similarly
But how fast is it actually?
npm run benchmark to find out for yourself how it stacks up against Moment for various formatting tokens. On the whole speed-date is ~15-20x faster than Moment with repeated use of the same formatter function. Check out benchmark results for an example run.
Is there any error handling or input validation?
Nope. None at all. In the interests of performance, speed-date assumes that you're always passing in valid Date objects and sane formatting strings. If you give it an invalid Date, it won't handle it nicely, like how Moment returns "Invalid date". It'll probably return something with
undefined all over the place. If you give it something that isn't a Date object at all, there's no telling what it'll do. It might return complete garbage, it might throw an error, it might set fire to your dog. So be careful.