Benchmark.js
by sperske
HTML
<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>
JavaScript
const data = new Map();
const DATA_SIZE = 100000;
for(let i = 0; i < DATA_SIZE; i++) {
data.set(i, i.toString());
}
function testHasFunction()
{
for(let i = 0; i < DATA_SIZE; i++) {
const h = data.has(i);
if(!h) {
throw new Error('Failed to read value!');
}
}
}
function testInOperator()
{
for(let i = 0; i < DATA_SIZE; i++) {
const h = (i in data);
if(!h) {
throw new Error('Failed to read value!');
}
}
}
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('testHasFunction', testHasFunction)
.add('testInOperator', testInOperator)
// 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 });
};