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/afternoonshore Jun 22 '16

Semi-newbie? Seems like you know

(typeof null); // "object"
(typeof undefined); // "undefined"

// anyway, the bug is that ...

for (let i = 0; i < 10000; i++){
    console.log(typeof null === 'undefined');
}

// somehow returns 50% false and 50% true in Chrome yet

for (let i = 0; i < 10000; i++){
    console.log(typeof null === typeof undefined);
}

// returns 100% false, so you're right in your suggestion ... of course traditional use of var ...

for (var i = 0; i < 10000; i++){
    console.log(typeof null === 'undefined');
}

// returns 100% false ... interestingly enough, declaring let before the loop ...

let i;
for (i = 0; i < 10000; i++){
    console.log(typeof null === 'undefined');
}

// returns 100% false ................ what a crazy weird error

1

u/vooydzig Jun 23 '16

Semi-newbie as in "I've been programming for some time now, not necessarily in JS" ;) it's just that I don't why you'd want to check types by string comparison not types comparison.

1

u/izuriel Jun 23 '16

Now it's obvious you haven't messed with JS, there aren't really any types to compare other than strings. The typeof "operator" returns a string value (see here) so that's why there is string comparison. Of course I've never seen an isUndefined that only checked against undefined. For all intents and purposes null is usually considered "undefined." But that doesn't change the fact this is a weird bug.

2

u/vooydzig Jun 24 '16

Yeah, i get all that. What I mean is: let say I want to check if variable type is number. I'd rather do typeof var_name === typeof 0 than typeof var_name === "Number". My reasoning is that on both sides of comparison I'd use operation to get type(as string) instead of hardcoding the type, because if someone changes specs for JS(let say ES9?) and rename number type to "whatever" first code would still work. As I've said I really don't write much JS so maybe there are some downsides to what I suggest here.

2

u/izuriel Jun 24 '16

Not a bad point. I never questioned why strings are typically used. This is just the way I've always seen it.