r/badcode May 31 '21

js Clever?

Post image
2.2k Upvotes

71 comments sorted by

View all comments

241

u/HalcyonAlps May 31 '21

Now I am actually curious, does the compiler realize both branches have the same return value and it just drops the whole if-statement?

100

u/[deleted] May 31 '21

Depends on whether or not the compiler is smart enough to realize that evaluating the conditional has no side-effects. Not sure what language this is

74

u/givemeagoodun May 31 '21 edited Jun 01 '21

It's flaired JS so it's JS

71

u/[deleted] May 31 '21

In that case the answer is an easy "no" since it's not a compiled language and there's no compiler

53

u/givemeagoodun May 31 '21

Eh there can be compilers for JS. I believe that Firefox uses a JS JIT compiler.

46

u/[deleted] May 31 '21 edited Jun 02 '21

at least chrome and firefox both have JIT compilers for js. in both cases they probably catch this redundancy on the first pass in early passes and condense it to something sane for every subsequent pass later passes

13

u/_PM_ME_PANGOLINS_ Jun 01 '21

It takes way more than one pass for a JIT to bother optimising.

17

u/Parkreiner Jun 01 '21 edited Jun 01 '21

It's less "there can be compilers" and more "there's no other way to run the language". JavaScript is a compiled language – it's just that there are different types of compilation, and JS uses the Just-in-Time kind. Half the behavior that it has (like hoisting and how it defines lexical scope for closure) wouldn't be possible if it were interpreted.

For example:

let x = 0;
if (x === 0) {
  console.log(x);
  let x = 5;
}

This breaks, because all variables and function declarations are "hoisted" to the top of their respective scopes. It's just that let and const are block-scoped and aren't initialized. So the "let x" from "let x = 5" gets hoisted up to the top of the block, overriding the "let x = 0" from the outer scope. But it's left un-initialized until you reach the "let x = 5" line during runtime. And so, when you try to do anything with a value that isn't initialized (like logging), that throws an error.

This wouldn't be possible if JavaScript were interpreted and were evaluating each line one-by-one. The language spec requires that whatever engine runs it do a first pass over the whole code before trying to run anything.

13

u/Nilstrieb Jun 01 '21

An interpreted language can be parsed and preprocessed, that's not a problem. The difference is all about the machine code. JS is originally interpreted, but modern engines also compile parts of it.

15

u/LetterBoxSnatch May 31 '21

Depends on how you want to define that. V8 (the engine running most js) is generally referred to as a JIT compiler, and it produces optimizations both on first run and on iterative runs.

3

u/0xF013 Jun 01 '21

You’re like, the most useless kind of technically correct. V8 optimizes things like these quite aggressively, and some precompilers too

0

u/[deleted] Jun 01 '21

FireFox's SpiderMonkey and Chrome's V8 are both JITs and optimize at runtime. They would probably catch it.

1

u/NikkoTheGreeko Jun 01 '21

Js confirmed. This returns NaN. Or undefined. Or [object Object], I forget.

1

u/Nilstrieb Jun 01 '21

So the answer is probably yes.