11

Consider this code:

Test = function() {
}

t = new Test();

for (var i = 0; i < 8; i++) {
  result = t instanceof Test;
}

If you change the number of iterations from 8 to 9, the loop will suddenly take about 100 times longer to complete in the latest version of Firefox (41.0.1). I tested this on two different PCs and the magic limit is always 8.

Here is the JSPerf test that I used: http://jsperf.com/instanceof-8-times-vs-9-times

Does anyone have any idea why that might happen? It seems to be specific to instanceof. It does not happen if you do something else with the object, for example check a property.


Note: I also filed a Bugzilla bug about this.

5
  • 1
    Interestingly enough it does not occur when you do it inline 8/9 times: jsperf.com/instanceof-8-times-vs-9-times/3 . Also doesn't happen on Firefox 40.3.
    – ralh
    Oct 1, 2015 at 9:41
  • 1
    Doing a recursive loop into a function is apparently faster. Quirky.
    – Chris
    Oct 1, 2015 at 9:50
  • 1
    Thanks for opening a bug for this issue ( bugzilla.mozilla.org/show_bug.cgi?id=1210342 ). My blind guess would be that this is just the cost of the compilation of the baseline compiler which appear when we enter the loop one extra time. If you were to use the larger number of iterations, the cost of each iteration should be amortized.
    – nbp
    Oct 1, 2015 at 10:25
  • I tried it with various Firefox versions (20.0, 30.0, 35.0, 36.0, 36.0.4 and 37.0). It seems to be related to something added in version 37.0, older versions than 37 are going well. Where we can find the complete changelog for this version? the release notes does not help at all Oct 1, 2015 at 13:09
  • The time/iteration is about the same whether the loop has 9 iterations or 9000 iterations. This means that the reason is probably not an expensive operation performed once per loop. If that were the case, the 9000-iteration version would take less time/iteration. It's still possible, however, that there is an expensive operation performed once per test (in a test, a loop is executed many times). Oct 1, 2015 at 17:53

2 Answers 2

1

Jan de Mooij from the Mozilla team has posted some details in the Bugzilla thread. Here's my simplistic interpretation of his highly technical answers:

In the i < 8 case, Firefox is smart enough to hoist the result = t instanceof Test; statement out of the loop (according to my tests, it doesn't seem to omit it altogether). In the i < 9 case, it apparently doesn't do that optimization.

Why? The reason is not exactly clear, but it is probably related to the fact that 9 iterations is the threshold above which the function is considered "hot" enough to run it through the JIT compiler. The i < 8 case stays in the interpreter. (I don't see why JIT-ing would exclude hoisting, but apparently it does in the current version of the engine.)

Interestingly, the 8-iteration threshold doesn't seem to be universal. For example, if we replace our own prototype (Test) with a built-in prototype (e.g. CustomEvent), hoisting doesn't seem to occur regardless of the number of iterations (relevant JSPerf):

for (var i = 0; i < 8; i++) { //or i < 9
  t instanceof CustomEvent;
}

Coming back to the original code using the Test prototype, why is performance so bad in the i < 9 case? This is related to how JSPerf works. The "setup" code is not just executed once – it is run once "per test". Every time you click Run, JSPerf runs hundreds of "tests", each test comprising thousands of iterations. So the setup code is run hundreds of times. This means that there are hundreds of different prototype objects named Test in the program, all created with the line:

Test = function(){
}

The Ion JIT optimizing compiler can easily optimize the case in which we are using instanceof on the same prototype object many times (as we do with CustomEvent in this test case), but when it notices that there is more than one object with the same name, apparently it throws its hands in the air.

Jan has rightly pointed out that this is not likely to affect too many real-world scripts because normally a single identifier is associated with a single prototype object (e.g. you have a class Foobar, which is defined only once and never re-defined). But JSPerf re-defines prototypes hundreds of times. It seems to me that this fact casts serious doubt on all published JSPerf results which include prototype definitions, except those which explicitly avoid redefinition by using globals (as in this test case) – which is perhaps the most important conclusion from all this.

For example, the JSPerf tests linked from this question: Is using instanceof operator in javascript a performance issue? are probably worthless, since they all define prototypes in the setup code.

-2

When the value is 9 you loop 10 times so the 100x is presumably 10^2 - i.e. two chars rather than one. One could also determine whether doing it 100 times will result in a 10^3 slow down. Sounds nuts, but this Javascript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.