Skip to content

Instantly share code, notes, and snippets.

@vatson
Created January 17, 2018 13:33
Show Gist options
  • Save vatson/cd5a9c7b8c98094e68200b69f436e869 to your computer and use it in GitHub Desktop.
Save vatson/cd5a9c7b8c98094e68200b69f436e869 to your computer and use it in GitHub Desktop.
Check the speed the rounding inside loop and in loop's condition
const Benchmark = require('benchmark')
const suite = new Benchmark.Suite()
suite
.add('With rounding inside loop', () => {
let i = 0
let step = 0.5
while(i < 1000) {
i += Math.round(step)
}
})
.add('With rounding in condition', () => {
let i = 0
let step = 0.5
while(Math.round(i) < 1000) {
i += step
}
})
.on('cycle', event => console.log(String(event.target)) )
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'))
})
.run({ 'async': true })
@vatson
Copy link
Author

vatson commented Jan 17, 2018

Results:

With rounding inside loop x 1,352,140 ops/sec ±0.80% (89 runs sampled)
With rounding in condition x 144,753 ops/sec ±0.78% (89 runs sampled)
Fastest is With rounding inside loop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment