Leonardo Mezavilla
Leonardo Mezavilla

Reputation: 101

How do I compare the performance of two similar bits of code?

I tried to find with one is faster, == or ===. I'm not interested in answers, I want to know how to test it.

I create one code to measure, but the difference between is so near, then I couldn't prove the answer.

I changed my CPU speed, and run this code. But every time shows a different value. Still inconclusive which one is faster.

let B = new Array(1000000).fill(3);

console.time("teste 2 ");
test2(B);
console.timeEnd("teste 2 ");

console.time("teste 3 ");
test3(B);
console.timeEnd("teste 3 ");

function test2(B) {
    for (i in B) {
        if (NaN == B[i]) { }
    }
}

function test3(B) {
    for (i in B) {
        if (NaN === B[i]) { }
    }
}

enter image description here

Upvotes: 0

Views: 578

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1075159

To find out whether there's any significant difference (there isn't, in this case¹) and if so, which is faster (neither, in this case), you need a lot more iterations, and a lot more test runs. When measuring something very, very small, you need lots of data to make the signal rise above the noise.

Ensure that all background processes you can turn off on your machine are turned off, and then run thousands of iterations of each version of the code, alternating between them, one after another.

I would also suggest testing something that isn't a special case. NaN in comparisons is a special case: The comparison is always false. So instead of testing NaN against 3, I'd test 3 and some number other than 3.

There are tools designed to do this for you. One of the most famous is jsPerf.com. Here's a test I did there. But even jsPerf is focussed on getting a result in a reasonable period of time (less than a minute), whereas a proper test to detect a very small difference might well be several minutes or even hours long.

BUT: Any difference between == and === is going to be so small there's no point in testing it at all. You almost certainly have bigger fish to fry.


¹ Why isn't there a difference? Because the two things you're comparing are both numbers, so the comparisons do exactly the same thing. The claim you may have heard that === is faster than == is rooted in the fact that == coerces/converts operands of differing types to a common type. When that coercion/conversion isn't necessary, == and === do exactly the same thing.

Upvotes: 3

Related Questions