Modulo vs explicit comparison

Benchmark created on


Setup

const increment = 3
const maxCount = 50
const maxIterations = 5000

Test runner

Ready to run.

Testing in
TestOps/sec
Modulo every tick
let iterations = 0
let count = 0
while(iterations++ < maxIterations) {
	count += increment
	count = count % maxCount
}
ready
Explicit comparison then modulo
let iterations = 0
let count = 0
while(iterations++ < maxIterations) {
	count += increment
	if (count > maxCount) {
		count = count % maxCount
	}
}
ready
Explicit comparison with Math.abs then modulo
let iterations = 0
let count = 0
while(iterations++ < maxIterations) {
	count += increment
	if (Math.abs(count) > maxCount) {
		count = count % maxCount
	}
}
ready
Explicit comparison then subtract
let iterations = 0
let count = 0
while(iterations++ < maxIterations) {
	count += increment
	if (count > maxCount) {
		count = maxCount - count
	}
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.