Skip to content

Instantly share code, notes, and snippets.

@thotzl
Created August 4, 2016 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thotzl/86e8ae931fff030a1201befa961eee76 to your computer and use it in GitHub Desktop.
Save thotzl/86e8ae931fff030a1201befa961eee76 to your computer and use it in GitHub Desktop.
jsperf alternative (till v2 is ready)
<ul id='cycleResults'>
</ul>
<div id="result">
</div>
<br>
<button id="btn">
Run Tests
</button>
var cycleResults = document.getElementById('cycleResults'),
result = document.getElementById('result'),
btn = document.getElementById('btn'),
tests = {
test1: function() {
console.log('test1');
return this;
},
test2: function() {
console.log('test2');
return this;
}
}
// BENCHMARK ====================
btn.onclick = function runTests() {
btn.setAttribute('disable', true);
cycleResults.innerHTML = '';
result.textContent = 'Tests running...';
var suite = new Benchmark.Suite;
for (var test in tests) {
if (tests.hasOwnProperty(test)) {
suite.add(test, tests[test])
}
}
// add tests
suite.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': false
});
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment