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

Show parent comments

1

u/[deleted] Jun 21 '16

unlike x === undefined, typeof x === "undefined" doesn't throw a ReferenceError if the variable doesn't exist

it feels kind of hacky though and i consider it a bad part(tm) of the language as i can't think of a scenario where it couldn't be replaced with either "varname" in global or varname !== undefined

1

u/qwzxercv Jun 21 '16

You should not be attempting to use variables that do not exist. That's an error in any language. I don't think this is reason enough to avoid x === undefined.

2

u/[deleted] Jun 21 '16

Have you written JavaScript for the web before? How can you tell whether or not your dependencies load? For example, let's say you have a <script> tag that loads foo.js, which exports the foo object. How can you tell whether or not the HTTP request failed, and whether or not you have foo in scope?

The answer:

if (typeof foo !== "undefined") {
    // we have foo
} else {
    // something went wrong
}

You could also solve this specific problem with jumping to the global scope and checking a property with a hacky one-liner (below), but it's intrinsically janky, non-portable, and it isn't The Right Way™.

(function () { return this; })().hasOwnProperty('foo')

4

u/wyqydsyq Jun 22 '16

Dependency issues are mostly resolved in modern web dev thanks to build tools like webpack, browserify etc. (typeof x !== 'undefined') is mostly useful these days for checking existence of properties on dynamically generated or user-input objects, or for polyfills to test whether the browser implements a feature.

2

u/[deleted] Jun 22 '16

Absolutely, my comment was probably over-simplified, but I was trying not to confuse them further. Thanks!