r/javascript • • Jun 20 '16

Javascript developers, be warned about this crazy JIT bug in V8!

Last week I've spent a full day to fix very strange behavior in a single page application for my customer that only happens on Chrome 51 (maybe 52, but not in Canary 53).

After a while our SPA started behaving incorrectly...

It turned out to be a nasty bug in the V8 JIT engine.

Try the following in Chrome's developer console:

function foo() {
  return typeof null === "undefined";
}

for(var i=0; i<10000; ++i) console.log(foo())

This should of course always print false, but in Chrome 51, it prints true after about about 100 iterations (it varies...)

I want to warn other web-developers for this bug, to avoid many $$$ being wasted (or becoming insane ;-))

 

EDIT: The following - more realistic case that we had in third-party library code - will also fail, but after about 10 thousand iterations:

function isUndefined(thing){
    return typeof thing === "undefined";
}

for(var i=0; i<100000; i++) console.log(isUndefined(null));

 

EDIT: The bug also manifests itself with the more common pattern

typeof x !== "undefined"

UPDATE: I just tried this with the latest stable Chrome (52.0.2743.82), and it now works correctly :)

482 Upvotes

117 comments sorted by

View all comments

37

u/theofREDDIT Jun 20 '16

Good job digging that deep in one day !!

11

u/ziriax Jun 21 '16

Hey thanks! Maybe I was overconfident that my code had to work correctly, LOL ;-) No, the moment I noticed that stepping through the code gave a different behavior, I realized I had to search for compiler bugs. I experienced the same problems ~30 years ago with C/C++ compilers (it was one of the reasons I kept programming in assembly language for so long). To be honest, since we switched from Silverlight to HTML5+ Javascript at smartphoto.com, we have to workaround browser and driver bugs almost weekly. Standard features that worked fine tend to break in new releases. It's a living organism, and it makes me wonder about modern QA...

5

u/squidc Jun 21 '16

That's interesting. I've been writing javascript for more than a decade, and I've never ever come across anything like what you recently found.

I do come across the occasional rendering inconsistencies, however.

1

u/keystorm Jun 23 '16

I guess never before have we had such a fierce commercial competition between compilers. Short life cycles, performance oriented, over the subset of a language that constantly changes its syntax...

It must be hard.