Bench i + b
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/2.1.4/benchmark.min.js"></script>
TypeScript
var suite = new Benchmark.Suite;
var i = 0;
var b = confirm('inject a true/false value');
// add tests
suite
.add('i + b', function() {
i = i + b;
})
.add('i + Number(b)', function() {
i = i + Number(b);
})
.add('i + (b ? 1 : 0)', function() {
i = i + (b ? 1 : 0);
})
.add('i + (b as unknown as number)', function() {
i = i + (b as unknown as number);
})
// add listeners
.on('cycle', function(event) {
console.log(i, String(event.target));
i = 0;
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
// run async
.run({
'async': true
});