-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.tsx
215 lines (188 loc) · 4.54 KB
/
bench.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import * as PropTypes from 'prop-types';
import __, { setLang } from '../index';
import { translate, Trans } from 'react-i18next';
import { IntlProvider, FormattedMessage } from 'react-intl';
import { i18next, langDicts } from './i18n.config';
import { Suite } from 'benchmark';
type i18nextProps = {
t: (id: string) => string;
i18n: {
changeLanguage: (lang: string) => void;
};
}
type BenchProps = {
name: string;
count: number;
lang: string;
langDict: any;
}
class Link extends React.Component<{to: string}> {
render() {
return <a href={this.props.to}>{this.props.children}</a>;
}
}
const Bench = {
'react-i18next': {
setLang: (lang) => i18next.changeLanguage(lang),
Block: translate('translations')((props: BenchProps & i18nextProps) => (
<ReactI18Next data={props}/>
)),
},
'react-intl': {
setLang: (lang, render) => render(lang),
Block: (props: BenchProps) => (
<IntlProvider locale={props.lang} messages={props.langDict}>
<ReactIntl data={props}/>
</IntlProvider>
),
},
'tx-i18n': {
setLang: (lang, render) => {
setLang(lang);
render(lang)
},
Block: (props: BenchProps) => (
<TxI18n data={props}/>
),
},
};
class ReactI18Next extends React.Component<{data: BenchProps & i18nextProps}> {
render() {
const {name, count, t} = this.props.data;
return <div>
<h1>{t('simpleContent')}</h1>
<div>
<Trans i18nKey="userMessagesUnread" count={count}>
Hello <strong title={t('nameTitle')}>{{name}}</strong>, you have unread messages: {{count}}. <Link to="#msgs">Go to messages</Link>.
</Trans>
</div>
</div>;
}
}
class ReactIntl extends React.Component<{data: BenchProps}> {
static contextTypes = {
intl: PropTypes.object.isRequired,
};
render() {
const {
name,
count,
} = this.props.data;
const {intl} = this.context;
return <div>
<h1>
<FormattedMessage
id="simpleContent"
defaultMessage={`Just simple content`}
/>
</h1>
<div>
<FormattedMessage
id="userMessagesUnread"
defaultMessage={`Hello {name}, you have unread messages: {unreadCount, number}.`}
values={{
name: <strong title={intl.messages.nameTitle}>{name}</strong>,
unreadCount: count,
}}
/>
{' '}
<Link to="#msgs">
<FormattedMessage
id="goTo"
defaultMessage={`Go to messages`}
/>
</Link>.
</div>
</div>
}
}
const TxI18n = ({data:{name, count}}: {data:BenchProps}) => (
<div>
<h1>{__('Just simple content')}</h1>
{__.jsx(
'Hello <1><#2></1>, you have unread messages: <#3>. <4>Go to messages</4>.',
[
{type: 'div', props: null},
{type: 'strong', props: {title: __('this is your name')}},
name,
count,
{type: Link, props: {to: '#msg'}},
]
)}
</div>
);
const root = document.getElementById('root');
const report = document.getElementById('report');
const print = (v: string) => report.appendChild(document.createTextNode(v + '\n'));
function renderTest(name: string, Target: any) {
const container = document.createElement('div');
const render = (lang: keyof typeof langDicts) => ReactDOM.render(
<Target.Block
name="RubaXa"
count={Math.random()}
lang={lang}
langDict={langDicts[lang]}
/>,
container,
);
root.insertBefore(container, root.firstChild);
root.insertBefore(document.createTextNode(`>>>> ${name} <<<<`), root.firstChild);
return {
setLang: Target.setLang,
render,
};
}
let curLang = 'en' as keyof typeof langDicts;
window['define'] = {
amd: {},
};
[
{
name: 'render(en)',
test: (box) => {
box.render('en');
},
},
{
name: 'render(ru)',
before: (box) => box.setLang('ru', box.render),
test: (box) => {
box.render('ru');
},
},
{
name: 'switchLang()',
before: (box) => box.render(curLang),
test: (box) => {
curLang = curLang === 'ru' ? 'en' : 'ru';
box.setLang(curLang, box.render);
},
},
].reduce((queue, spec) => {
return queue.then(() => new Promise<void>(resolve => {
const suite = new Suite();
Object.keys(Bench).forEach(key => {
const item = renderTest(key, Bench[key]);
if (spec.before) {
spec.before(item);
}
suite.add(key, () => {
spec.test(item);
});
});
suite.on('cycle', (evt) => {
print(` - ${evt.target}`);
});
suite.on('complete', function () {
print('-----------------');
print(`Fastest is ${this.filter('fastest').map('name')}`);
print('\n\n');
resolve();
});
print(`Start: ${spec.name}`);
print('-----------------');
suite.run({ async: true });
}));
}, Promise.resolve());