Math.abs vs two comparisons

Benchmark created by Mike on


Description

Is it faster to compare against the absolute value of a number or make two comparisons?

Test runner

Ready to run.

Testing in
TestOps/sec
Math.abs
var sign = (Math.random() >= 0.5) ? 1 : -1,
    x = Math.random() * 2 * sign,
    y;
if (Math.abs(x) > 1) {
    y = Math.random();
}
ready
Comparisons
var sign = (Math.random() >= 0.5) ? 1 : -1,
    x = Math.random() * 2 * sign,
    y;
if (x > 1 || x < 1) {
    y = Math.random();
}
ready

Revisions

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

  • Revision 1: published by Mike on