forked from musterknabe/translate.js
-
Notifications
You must be signed in to change notification settings - Fork 6
/
benchmark.html
202 lines (177 loc) · 6.24 KB
/
benchmark.html
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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title>Translate.js Benchmark Testing</title>
</head>
<body>
<h1>Translate.js Benchmark Testing</h1>
<h2>Test Results:</h2>
<pre id="out">...nothing is happening...</pre>
<script src="node_modules/benchmark/benchmark.js"></script>
<script>
window.T = {}; // T contains all the versions of translate.js we want to test
window.module = { exports:{} }; // to capture module.exports from older versions of translate.js...
</script>
<script src="https://rawgit.com/StephanHoyer/translate.js/v0.1.0/index.js"></script>
<script>T['0.1.0'] = module.exports;</script>
<!--
<script src="https://rawgit.com/StephanHoyer/translate.js/v0.2.0/index.js"></script>
<script>T['0.2.0 - Speed-up simple strings'] = module.exports;</script>
-->
<script src="https://rawgit.com/StephanHoyer/translate.js/v0.2.2/index.js"></script>
<script>T['0.2.2 - Speed-up replacements'] = module.exports;</script>
<script src="https://rawgit.com/StephanHoyer/translate.js/v0.3.1/index.js"></script>
<script>T['0.3.1 - Drop Namespace support'] = module.exports;</script>
<script src="https://rawgit.com/StephanHoyer/translate.js/v0.3.2/index.js"></script>
<script>T['0.3.2 - Simplify assemble function'] = module.exports;</script>
<script src="https://rawgit.com/StephanHoyer/translate.js/v0.4.0/index.js"></script>
<script>T['0.4.0 - Introducing t.arr()'] = module.exports;</script>
<!--
<script src="https://rawgit.com/StephanHoyer/translate.js/master/index.js"></script>
<script>T['Current Master'] = module.exports;</script>
-->
<script src="index.js"></script>
<script>T['Local file'] = module.exports;</script>
<script>
window.translatejs = undefined;
var out = document.getElementById('out');
out.innerHTML = '--------------------------------------------\n';
// Collection of different types of translation keys that get fed into the testFuncs below.
var testKeys = {
vanilla: {
'a': 'Lorem',
'b': 'Ipsum',
'c': 'Dolor',
'd': 'Sit',
},
singleKeyed: {
'a': 'Lor {0} em',
'b': 'Ipsum {1} dolor ',
'c': '{2} Dolor',
'd': 'S {0} it ',
},
multiKeyed: {
'a': '{2} Lor {0} em {1}',
'b': 'Ipsum {0}, {1} {2} ',
'c': '{0} Dolor {1}, {2} ',
'd': 'S {0} i {1} t {2} ',
},
}
// var replacements = { '0':'Lorem', '1':'ipsum', '2':'dolor' };
var replacements = ['Lorem','ipsum','dolor'];
// var pluralizer = function ( n ) { return n!=1 ? 'p' : 's'; };
// Collection of test functions
var testFuncs = {
simple: function ( t, keys ) {
return function(){
for (var key in keys)
{
t(key);
t(key);
t(key);
t(key);
}
};
},
replacement: function ( t, keys ) {
return function(){
for (var key in keys)
{
t(key, replacements);
t(key, replacements);
t(key, replacements);
t(key, replacements);
}
};
},
replacementArray: function ( t, keys ) {
return t.arr && function(){
for (var key in keys)
{
t.arr(key, replacements);
t.arr(key, replacements);
t.arr(key, replacements);
t.arr(key, replacements);
}
};
},
}
// Named test definitions - combining testFuncs and testKeys to be run.
var tests = [
{
// disabled: true,
name: 'Vanilla keys',
keys: testKeys.vanilla, // or self-running function;
// opts: undefined,
fn: testFuncs.simple,
},
{
// disabled: true,
name: 'Vanilla keys - unnecessary options',
keys: testKeys.vanilla, // or self-running function;
// opts: undefined,
fn: testFuncs.replacement,
},
{
// disabled: true,
name: 'Replacement single keys',
keys: testKeys.singleKeyed, // or self-running function;
// opts: undefined,
fn: testFuncs.replacement,
},
{
//disabled: true,
name: 'Replacement multiple keys',
keys: testKeys.multiKeyed, // or self-running function;
// opts: undefined,
fn: testFuncs.replacement,
},
{
//disabled: true,
name: 'Replacement multiple keys (VDOM)',
keys: testKeys.multiKeyed, // or self-running function;
// opts: undefined,
fn: testFuncs.replacementArray,
},
];
// The test runner - loops through `tests` Array and runs one test at a time
var testIndex = 0;
var runNext = function () {
var test = tests[testIndex++];
if ( test)
{
// Skip over disabled tests
if (test.disabled) {
setTimeout(runNext, 0);
return;
}
out.innerHTML += test.name + ':\n';
var suite = new Benchmark.Suite;
for (var version in T)
{
var testFn = test.fn( T[version]( test.keys, test.opts ), test.keys );
if ( testFn ) {
suite.add( version, testFn );
}
}
suite
.on('cycle', function(e) {
out.innerHTML += ' * ' + e.target.toString().split('±')[0].replace(' x ',': ') + '\n';
})
.on('complete', function() {
// out.innerHTML += '\n Fastest: ' + this.filter('fastest').pluck('name') + '\n';
out.innerHTML += '\n';
out.innerHTML += '--------------------------------------------\n';
runNext();
})
// run
.run({ 'async': true });
}
};
// Run the first test!
runNext();
</script>
</body>
</html>