forked from hasharray/jsperf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
365 lines (307 loc) · 10 KB
/
index.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
---
---
{% assign names = "" | split: "|" %}
{% for benchmark in site.benchmarks %}
{% for test in benchmark.tests %}
{% assign names = names | push: test.name %}
{% endfor %}
{% endfor %}
{% assign names = names | uniq %}
<datalist id="names">
{% for name in names %}
<option value="{{ name }}" />
{% endfor %}
</datalist>
<form method="post" action="https://api.postbot.pw/contents/hasharray/jsperf/benchmarks">
<section>
<p>
Submit a new benchmark to measure and compare the relative performance
difference between different snippets of JavaScript code.
</p>
<dl>
<dt>
<label>Title</label>
</dt>
<dd>
<input name="data[title]" required/>
</dd>
</dl>
<dl>
<dt>
<label>Title</label>
</dt>
<dd>
<input name="data[slug]" required placeholder="Define the test names below to define the title"/>
</dd>
</dl>
<dl>
<dt>
<label>Description</label>
</dt>
<dd>
<textarea name="data[content]" rows="5" placeholder="Describe the benchmark with a few words"></textarea>
</dd>
</dl>
<dl>
<dt>
<label>Setup</label>
</dt>
<dd>
<textarea name="data[setup]" rows="5" placeholder="Define the variables and functions that need to be available to the code in the test cases below"></textarea>
</dd>
</dl>
</section>
<section>
<h2 id="tests">Tests</h2>
<ol>
{% for number in (0...1) %}
<li>
<dl>
<dt>
<label>Name</label>
</dt>
<dd>
<input name="data[tests][][name]" required placeholder="Define a name for the test case"/>
</dd>
</dl>
<dl>
<dt>
<label>Code</label>
</dt>
<dd>
<textarea name="data[tests][][code]" rows="5" required placeholder="Define the code for the test case"></textarea>
</dd>
</dl>
</li>
{% endfor %}
</ol>
</section>
<section id="actions">
<button type="submit">Submit</button>
</section>
</form>
<section>
<h2>Recent Submissions</h2>
<ul>
{% for benchmark in site.benchmarks %}
{% unless benchmark.title %}
{% continue %}
{% endunless %}
<li>
<a href="{{ benchmark.url | prepend: site.baseurl }}">{{ benchmark.title }}</a>
</li>
{% endfor %}
</ul>
</section>
<template>
<form oninput="onInput(event)" onsubmit="onSubmit(event)" method="post" action="https://api.postbot.pw/contents/hasharray/jsperf/benchmarks">
<input id="redirect" type="hidden" name="redirect_uri" value="{{ site.url | default: site.github.url }}" />
<input id="slug" type="hidden" name="data[slug]" required/>
<section>
<p>
Submit a new benchmark to measure and compare the relative performance
difference between different snippets of JavaScript code.
</p>
<dl>
<dt>
<label>Title</label>
</dt>
<dd>
<input id="title" name="data[title]" rows="5" readonly placeholder="Define the test names below which will automatically fill in the title" data-automatic/>
</dd>
</dl>
<dl>
<dt>
<label>Description</label>
</dt>
<dd>
<textarea id="content" name="data[content]" rows="5" placeholder="Describe the benchmark with a few words"></textarea>
</dd>
</dl>
<dl>
<dt>
<label>Setup</label>
</dt>
<dd>
<textarea id="setup" oninput="onCodeInput(event)" name="data[setup]" rows="5" placeholder="Define the variables and functions that need to be available to the code in the test cases below"></textarea>
</dd>
</dl>
</section>
<section>
<h2 id="tests">Tests</h2>
<ol>
{% for number in (0...1) %}
<li>
<dl>
<dt>
<label>Name</label>
</dt>
<dd>
<input list="names" name="data[tests][{{ number }}][name]" required placeholder="Define a name for the test case"/>
</dd>
</dl>
<dl>
<dt>
<label>Code</label>
</dt>
<dd>
<textarea oninput="onCodeInput(event)" name="data[tests][{{ number }}][code]" rows="5" required placeholder="Define the code for the test case"></textarea>
</dd>
</dl>
</li>
{% endfor %}
<li>
<button class="small" onclick="onInsertClick(event)" type="button">Insert code snippet</button>
</li>
</ol>
</section>
<section id="actions">
<button type="submit">Submit</button>
</section>
</form>
<template id="item">
<li>
<button class="small" onclick="onRemoveClick(event)" type="button">Remove code snippet</button>
<dl>
<dt>
<label>Name</label>
</dt>
<dd>
<small></small>
</dd>
<dd>
<input list="names" name="data[tests][0][name]" required placeholder="Define a name for the test case"/>
</dd>
</dl>
<dl>
<dt>
<label>Code</label>
</dt>
<dd>
<textarea oninput="onCodeInput(event)" name="data[tests][0][code]" rows="5" required placeholder="Define the code for the test case"></textarea>
</dd>
</dl>
</li>
</template>
<script type="text/javascript">
var data = {};
if (localStorage.getItem('form')) {
data = JSON.parse(localStorage.getItem('form'));
}
Object.keys(data).forEach(function(name) {
var element = document.querySelector('[name="' + name + '"]');
if (element == null) {
var template = document.querySelector('#item');
var container = document.querySelector('#tests + ol');
var clone = document.importNode(template.content, true);
container.insertBefore(clone, container.lastElementChild);
var children = Array.from(container.children);
children.forEach(function(child, index) {
var elements = child.querySelectorAll('[name]');
elements.forEach(function(element) {
element.name = element.name.replace(/\d/, index);
});
});
element = document.querySelector('[name="' + name + '"]');
}
element.value = data[name];
});
function onInsertClick(event) {
var template = document.querySelector('#item');
var element = event.target.parentElement;
var container = element.parentElement;
var clone = document.importNode(template.content, true);
container.insertBefore(clone, container.lastElementChild);
var children = Array.from(container.children);
children.forEach(function(child, index) {
var elements = child.querySelectorAll('[name]');
elements.forEach(function(element) {
element.name = element.name.replace(/\d/, index);
});
});
}
function onRemoveClick(event) {
var element = event.target.parentElement;
var container = element.parentElement;
container.removeChild(element);
if (localStorage.getItem('form')) {
var data = JSON.parse(localStorage.getItem('form'));
var elements = Array.from(element.querySelectorAll('[name]'));
elements.forEach(function(element) {
element.value = '';
delete data[element.name];
});
localStorage.setItem('form', JSON.stringify(data));
}
var children = Array.from(container.children);
children.forEach(function(child, index) {
var elements = child.querySelectorAll('[name]');
elements.forEach(function(element) {
element.name = element.name.replace(/\d/, index);
});
});
}
function onCodeInput(event) {
var element = event.target;
var code = setup.value;
if (element !== setup) {
code = setup.value + '\n' + element.value;
}
var validate = new Function(code);
try {
validate();
element.setCustomValidity('');
} catch(error) {
element.setCustomValidity(error);
}
}
function onInput(event) {
var element = event.target;
var data = {};
if (localStorage.getItem('form')) {
data = JSON.parse(localStorage.getItem('form'));
}
if (/name/.test(element.name)) {
var elements = Array.from(document.querySelectorAll('[name*=name]'));
title.value = elements.map(function(element) {
return element.value;
}).filter(function(value) {
return value.length > 0;
}).join(' vs ');
data[title.name] = title.value;
}
data[element.name] = element.value;
localStorage.setItem('form', JSON.stringify(data));
}
function onSubmit(event) {
localStorage.removeItem('form');
var slug = document.querySelector('[name="data[slug]"]');
slug.value = title.value.replace(/\W+/g, '-').toLowerCase();
var redirect = document.querySelector('[name="redirect_uri"]');
redirect.value = location.href + slug.value;
var elements = Array.from(document.querySelectorAll('[name]'));
elements.forEach(function(element) {
element.name = element.name.replace(/\d/, '');
});
var tests = Array.from(document.querySelectorAll('ol li')).filter(function(element) {
return element.querySelector('textarea');
});
localStorage.setItem(slug.value, JSON.stringify({
setup: setup.value,
content: content.value,
tests: tests.map(function(element) {
return {
name: element.querySelector('input').value,
code: element.querySelector('textarea').value,
};
}).filter(Boolean),
}));
}
</script>
</template>
<script type="text/javascript">
document.querySelector('form').parentElement.replaceChild(
document.importNode(document.querySelector('template').content, true),
document.querySelector('form')
);
</script>