0

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

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 2
    Unrelated to your actual question, two notes on the code itself: 1. Your code is falling prey to what I call [The Horror of Implicit Globals](http://blog.niftysnippets.org/2008/03/horror-of-implicit-globals.html). You need to declare your variables (such as `i`). 2. `for-in` isn't meant for looping through arrays, [more here](https://stackoverflow.com/questions/9329446/for-each-over-an-array-in-javascript/9329476#9329476). – T.J. Crowder Dec 07 '18 at 10:37
  • better use `===` because it check value and type. `==` is value only to check it. – PPShein Dec 07 '18 at 10:40
  • @PyaePhyoeShein - The question isn't about which is better, and "better" is a subjective term. – T.J. Crowder Dec 07 '18 at 10:43
  • 1
    If you run a test which involves actual type coercion, `==` will definitely be slower https://jsfiddle.net/3rhz2jLb/ – CertainPerformance Dec 07 '18 at 10:46
  • Without type coercion, `===` seems *almost consistently*, *very slightly* faster on Chrome https://jsfiddle.net/3rhz2jLb/1/ (average of ~1000ms vs 970ms +- 20, or thereabouts, for me, over lots of runs, for me. Too much variance on FF 56 though) – CertainPerformance Dec 07 '18 at 10:48

1 Answers1

3

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875