Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLukas Eipert <leipert@gitlab.com>2019-01-22 02:14:21 +0300
committerLukas Eipert <leipert@gitlab.com>2019-01-22 02:14:21 +0300
commit6e4dac3589d2918f565cf4dc4771df875a6836e2 (patch)
treedc8e63bd1680acce666ff82103b49a1c7b2bfd9e
parentbd91bf1ea8cba1bc3aaaf10bf7bbc92ff64597d6 (diff)
Memoize timeago locales
Each localized function call of the timeago calls gettext 28! times. Even though gettext is rather fast, this escalates pretty quickly. We effectively memoize the locales, which results in a maximum of 28 function calls in total. For pages with normal data, less calls (< 10) are much more realistic.
-rw-r--r--app/assets/javascripts/lib/utils/datetime_utility.js124
1 files changed, 86 insertions, 38 deletions
diff --git a/app/assets/javascripts/lib/utils/datetime_utility.js b/app/assets/javascripts/lib/utils/datetime_utility.js
index 4b8f11ba7a6..4601bc77784 100644
--- a/app/assets/javascripts/lib/utils/datetime_utility.js
+++ b/app/assets/javascripts/lib/utils/datetime_utility.js
@@ -87,44 +87,92 @@ let timeagoInstance;
*/
export const getTimeago = () => {
if (!timeagoInstance) {
- const localeRemaining = (number, index) =>
- [
- [s__('Timeago|just now'), s__('Timeago|right now')],
- [s__('Timeago|%s seconds ago'), s__('Timeago|%s seconds remaining')],
- [s__('Timeago|1 minute ago'), s__('Timeago|1 minute remaining')],
- [s__('Timeago|%s minutes ago'), s__('Timeago|%s minutes remaining')],
- [s__('Timeago|1 hour ago'), s__('Timeago|1 hour remaining')],
- [s__('Timeago|%s hours ago'), s__('Timeago|%s hours remaining')],
- [s__('Timeago|1 day ago'), s__('Timeago|1 day remaining')],
- [s__('Timeago|%s days ago'), s__('Timeago|%s days remaining')],
- [s__('Timeago|1 week ago'), s__('Timeago|1 week remaining')],
- [s__('Timeago|%s weeks ago'), s__('Timeago|%s weeks remaining')],
- [s__('Timeago|1 month ago'), s__('Timeago|1 month remaining')],
- [s__('Timeago|%s months ago'), s__('Timeago|%s months remaining')],
- [s__('Timeago|1 year ago'), s__('Timeago|1 year remaining')],
- [s__('Timeago|%s years ago'), s__('Timeago|%s years remaining')],
- ][index];
-
- const locale = (number, index) =>
- [
- [s__('Timeago|just now'), s__('Timeago|right now')],
- [s__('Timeago|%s seconds ago'), s__('Timeago|in %s seconds')],
- [s__('Timeago|1 minute ago'), s__('Timeago|in 1 minute')],
- [s__('Timeago|%s minutes ago'), s__('Timeago|in %s minutes')],
- [s__('Timeago|1 hour ago'), s__('Timeago|in 1 hour')],
- [s__('Timeago|%s hours ago'), s__('Timeago|in %s hours')],
- [s__('Timeago|1 day ago'), s__('Timeago|in 1 day')],
- [s__('Timeago|%s days ago'), s__('Timeago|in %s days')],
- [s__('Timeago|1 week ago'), s__('Timeago|in 1 week')],
- [s__('Timeago|%s weeks ago'), s__('Timeago|in %s weeks')],
- [s__('Timeago|1 month ago'), s__('Timeago|in 1 month')],
- [s__('Timeago|%s months ago'), s__('Timeago|in %s months')],
- [s__('Timeago|1 year ago'), s__('Timeago|in 1 year')],
- [s__('Timeago|%s years ago'), s__('Timeago|in %s years')],
- ][index];
-
- timeago.register(timeagoLanguageCode, locale);
- timeago.register(`${timeagoLanguageCode}-remaining`, localeRemaining);
+ const memoizedLocaleRemaining = () => {
+ const cache = [];
+
+ return (number, index) => {
+ if (cache[index]) {
+ return cache[index];
+ }
+ let result = [];
+ if (index === 0) {
+ result = [s__('Timeago|just now'), s__('Timeago|right now')];
+ } else if (index === 1) {
+ result = [s__('Timeago|%s seconds ago'), s__('Timeago|%s seconds remaining')];
+ } else if (index === 2) {
+ result = [s__('Timeago|1 minute ago'), s__('Timeago|1 minute remaining')];
+ } else if (index === 3) {
+ result = [s__('Timeago|%s minutes ago'), s__('Timeago|%s minutes remaining')];
+ } else if (index === 4) {
+ result = [s__('Timeago|1 hour ago'), s__('Timeago|1 hour remaining')];
+ } else if (index === 5) {
+ result = [s__('Timeago|%s hours ago'), s__('Timeago|%s hours remaining')];
+ } else if (index === 6) {
+ result = [s__('Timeago|1 day ago'), s__('Timeago|1 day remaining')];
+ } else if (index === 7) {
+ result = [s__('Timeago|%s days ago'), s__('Timeago|%s days remaining')];
+ } else if (index === 8) {
+ result = [s__('Timeago|1 week ago'), s__('Timeago|1 week remaining')];
+ } else if (index === 9) {
+ result = [s__('Timeago|%s weeks ago'), s__('Timeago|%s weeks remaining')];
+ } else if (index === 10) {
+ result = [s__('Timeago|1 month ago'), s__('Timeago|1 month remaining')];
+ } else if (index === 11) {
+ result = [s__('Timeago|%s months ago'), s__('Timeago|%s months remaining')];
+ } else if (index === 12) {
+ result = [s__('Timeago|1 year ago'), s__('Timeago|1 year remaining')];
+ } else if (index === 13) {
+ result = [s__('Timeago|%s years ago'), s__('Timeago|%s years remaining')];
+ }
+ cache[index] = result;
+ return result;
+ };
+ };
+
+ const memoizedLocale = () => {
+ const cache = [];
+
+ return (number, index) => {
+ if (cache[index]) {
+ return cache[index];
+ }
+ let result = [];
+ if (index === 0) {
+ result = [s__('Timeago|just now'), s__('Timeago|right now')];
+ } else if (index === 1) {
+ result = [s__('Timeago|%s seconds ago'), s__('Timeago|in %s seconds')];
+ } else if (index === 2) {
+ result = [s__('Timeago|1 minute ago'), s__('Timeago|in 1 minute')];
+ } else if (index === 3) {
+ result = [s__('Timeago|%s minutes ago'), s__('Timeago|in %s minutes')];
+ } else if (index === 4) {
+ result = [s__('Timeago|1 hour ago'), s__('Timeago|in 1 hour')];
+ } else if (index === 5) {
+ result = [s__('Timeago|%s hours ago'), s__('Timeago|in %s hours')];
+ } else if (index === 6) {
+ result = [s__('Timeago|1 day ago'), s__('Timeago|in 1 day')];
+ } else if (index === 7) {
+ result = [s__('Timeago|%s days ago'), s__('Timeago|in %s days')];
+ } else if (index === 8) {
+ result = [s__('Timeago|1 week ago'), s__('Timeago|in 1 week')];
+ } else if (index === 9) {
+ result = [s__('Timeago|%s weeks ago'), s__('Timeago|in %s weeks')];
+ } else if (index === 10) {
+ result = [s__('Timeago|1 month ago'), s__('Timeago|in 1 month')];
+ } else if (index === 11) {
+ result = [s__('Timeago|%s months ago'), s__('Timeago|in %s months')];
+ } else if (index === 12) {
+ result = [s__('Timeago|1 year ago'), s__('Timeago|in 1 year')];
+ } else if (index === 13) {
+ result = [s__('Timeago|%s years ago'), s__('Timeago|in %s years')];
+ }
+ cache[index] = result;
+ return result;
+ };
+ };
+
+ timeago.register(timeagoLanguageCode, memoizedLocale());
+ timeago.register(`${timeagoLanguageCode}-remaining`, memoizedLocaleRemaining());
timeagoInstance = timeago();
}