Skip to content
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

Merged
merged 3 commits into from Jun 6, 2012

Conversation

timrwood
Copy link
Member

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 for L 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.

Still need to inline L LL LLL LLLL LT functions…
@timrwood
Copy link
Member Author

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,
Copy link
Contributor

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

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 =)

Copy link
Member Author

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!

http://jsperf.com/regex-optional-vs-length

@timrwood timrwood mentioned this pull request May 29, 2012
@timrwood
Copy link
Member Author

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 token, but that might be insufficiently named as parsing tokens are not affected. I don't plan on exposing parsing tokens (and it would be difficult with the incremental parser) but we may need to in the future...

moment.token('MM', 'p(t.month()+1,2)'); // set a format token
moment.token('MM'); // 'p(t.month()+1,2)' (get a format token)

@rockymeza
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants