New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add cached format functions for 3x faster formatting. #317
Conversation
Still need to inline L LL LLL LLLL LT functions…
Also, we can probably do something similar with the parser. |
@@ -23,7 +23,9 @@ | |||
aspNetJsonRegex = /^\/?Date\((\-?\d+)/i, | |||
|
|||
// format tokens | |||
formattingTokens = /(\[[^\[]*\])|(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|dddd?|do?|w[o|w]?|YYYY|YY|a|A|hh?|HH?|mm?|ss?|SS?S?|zz?|ZZ?|LT|LL?L?L?)/g, | |||
formattingTokens = /(\[[^\[]*\])|(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|dddd?|do?|w[o|w]?|YYYY|YY|a|A|hh?|HH?|mm?|ss?|SS?S?|zz?|ZZ?)/g, | |||
localFormattingTokens = /(LT|LL?L?L?)/g, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can save 1 byte by using /(LT|L{1,4})/g
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also believe that /L{1,4}/
works faster then /LL?L?L?/
.
And same pattern used in the line above. I'd say, abused =)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Performance looks about the same between /(LT|L{1,4})/g
and /(LT|LL?L?L?)/g
. The saved byte is good though!
For exposing formatting tokens, I was thinking of using this syntax. moment.formatToken('MM', 'p(t.month()+1,2)'); // set a format token
moment.formatToken('MM'); // 'p(t.month()+1,2)' (get a format token) Alternatively, we could use moment.token('MM', 'p(t.month()+1,2)'); // set a format token
moment.token('MM'); // 'p(t.month()+1,2)' (get a format token) |
I think that the cached formatters should go out in 1.7, but if we ever do do extensible formatting that should not. The 3x boost for 1.7 seems to be ready already |
Not ready to pull in, opening discussion.
TODO: inline L LL LLL LLLL LT tokens.
Some speed tests:
http://jsperf.com/date-formatting/8
http://jsperf.com/momentjs-cached-format-functions
All
make test-moment
tests are passing now, need to add support forL LL LLL LLLL LT
for all tests to pass.File size difference
-108b minified
+75b gzipped
Though there are probably some byte squeezing techniques we could add.