Skip to content

Instantly share code, notes, and snippets.

@leeoniya
Last active December 17, 2016 15:15
Show Gist options
  • Save leeoniya/8aa8bf7e59f83ebe8507970ec823436c to your computer and use it in GitHub Desktop.
Save leeoniya/8aa8bf7e59f83ebe8507970ec823436c to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head></head>
<body>
<ul id='cycleResults'></ul>
<div id="result"></div>
<br>
<button id="btn">Run Tests</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.2/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/2.1.2/benchmark.min.js"></script>
<script>
var len = 1e4;
var arr = new Array(len);
function fact(x, y) {
return {
x: x,
y: y,
};
}
function Obj(x, y) {
this.x = x;
this.y = y;
}
function factoryTest() {
for (var i = 0; i < len; i++)
arr[i] = fact(i, i+1);
}
function newTest() {
for (var i = 0; i < len; i++)
arr[i] = new Obj(i, i+1);
}
var cycleResults = document.getElementById('cycleResults'),
result = document.getElementById('result'),
btn = document.getElementById('btn'),
tests = {
factory: factoryTest,
newObj: newTest,
}
// 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': true
});
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment