既然 JPerf 已经关闭了,如何分析 Javascript?

你们中的一些人可能已经注意到 Jsperf下降了一段时间。但我还是需要对我的 Javascript 进行侧写。是否有可能在没有外部软件帮助的情况下进行理想的比较测试?

38903 次浏览

jsperf is based on benchmarkjs so using an online code editor (like jsfiddle, jsbin, plunker etc...) and including benchmarkjs as a library will do.

The only feature you won't have will be the compiled results for each browsers. This is just a temporary alternative.

Here is a jsfiddle template : https://jsfiddle.net/533hc71h/

But since we don't really care about HTML nor CSS I found plunker more suitable. Coupled with systemjs you can then separate your code into multiple files.

Here is the template : https://plnkr.co/edit/pJg5LsiSNqlc6immmGsW


Update

You really should only use those solution as quick temporary solution. As said on the comments for optimal result you had better run it locally, nowadays you can get a webserver like express or else running in sec.


Rather than "trick" Stack Overflow into allowing posting of these links, let's actually include some helpful code:

function test1() {


}


function test2() {


}


var cycleResults = document.getElementById('cycleResults');
var result = document.getElementById('result');
var btn = document.getElementById('btn');


// BENCHMARK ====================
btn.onclick = function runTests() {


btn.setAttribute('disable', true);
cycleResults.innerHTML = '';
result.textContent = 'Tests running...';


var suite = new Benchmark.Suite;


// add tests
suite
.add('test1', test1)
.add('test2', test2)
// add listeners
.on('cycle', function(event) {
var result = document.createElement('li');
result.textContent = String(event.target);


document.getElementById('cycleResults')
.appendChild(result);
})
.on('complete', function() {
result.textContent = 'Fastest is ' + this.filter('fastest').pluck('name');
btn.setAttribute('disable', false);
})
// run async
.run({
'async': true
});
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<ul id='cycleResults'>


</ul>
<div id="result">


</div>
<br>
<button id="btn">
Run Tests
</button>

There is also https://www.measurethat.net/ which allows you to create and run javascript benchmarks

I have incidentally come to know http://jsbench.github.io/.

It clearly reminds of good ol' jsperf.

You can save your benchmark, share them and they keep track of per-browser performance.

Here is one I just made up: For loop benchmark

(As a side note, you can only save a benchmark if you have a github account.)

Even though jsperf is online, if you still want to look at alternatives, I found https://jsben.ch/ to be quite useful and well designed.

I decided to build tool like this. First public beta is at https://jsbench.me

EDIT: 2020-07-12 - v1 released