Skip to content

Instantly share code, notes, and snippets.

@jareguo
Last active May 23, 2016 10:32
Show Gist options
  • Save jareguo/99cf83d1d851b53e37963316afadbcc7 to your computer and use it in GitHub Desktop.
Save jareguo/99cf83d1d851b53e37963316afadbcc7 to your computer and use it in GitHub Desktop.
在 jsperf 里动态加入一个新的测试项目
javascript:(function() {
var tbody = document.querySelector('table#test-table>tbody');
var lastTr = tbody.querySelector('tr:last-child');
var tr = document.createElement('tr');
tbody.appendChild(tr);
tr.innerHTML = lastTr.innerHTML;
var th = tr.querySelector('th');
th.id = 'title-' + tbody.childElementCount;
th.querySelector('div').innerHTML = 'New test<br>(Click to run again)';
var code = tr.querySelector('td.code');
code.innerHTML = '<textarea maxlength="16777215"></textarea>';
var textarea = code.childNodes[0];
var results = tr.querySelector('td.results');
results.id = 'results-' + tbody.childElementCount;
ui.add('New test', '');
var benchmark = ui.benchmarks[ui.benchmarks.length - 1];
textarea.onchange = function () {
benchmark.fn = textarea.value;
};
textarea.focus();
})();

jsperf 经常遇到无法提交修改过的代码,无法新增测试例的 bug。 使用这段脚本,可以让你在 jsperf 里动态加入一个新的测试片段,还能边修改边运行,不需要反复提交测试代码。 不过要注意的是,新增的测试代码不会得到保存。

在收藏夹中新增一个网页,将网址设为下面的源码,打开测试页面后直接点击收藏的网址即可。

效果: image

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