4 ms·
It's not a useless line, because prepack has no idea what Date.now() does (there are no guarantees in javascript that it hasn't been replaced with another funct
by shuzchen 9y ago
It's not a useless line, because prepack has no idea what Date.now() does (there are no guarantees in javascript that it hasn't been replaced with another function). It might mutate some global state somewhere, so the resulting code needs to call Date.now() as often as it would've if fib(10) was called. Basically the output is the unrolled version of the recursion, which cuts down on function invocation (comparatively expensive in dynamic languages).
If you replaced the line with say: `var y = 4;` you'll notice that it has been optimized out.
- KallDrexx 9y agoBut should it still unroll the recursive calls and balloon the code out just because a mutatable function call was made, or should it leave it as an actual recursive function call.
- magicalist 9y ago> Basically the output is the unrolled version of the recursion, which cuts down on function invocation (comparatively expensive in dynamic languages). You're right about why it's included, but this is a bug, not a feature. If this is a hot function it'll be optimized. The recursive call is a known, non-dynamic invocation so the function location will be inlined leaving just the fairly low function call overhead itself. There's a reason nobody does arbitrary-length loop unrolls. I would definitely bet that this is a bug to be fixed :) Right now you can see this if you call fib(20) in that example...the compiler times out while trying to unroll that far. Clearly that behavior won't stick around.
- warent 9y ago"prepack has no idea what Date.now() does" isn't quite true, because there's a typeof check happening on the return to ensure it's a number, meaning that some assumptions are definitely being made.
- simlrh 9y agoAlso the lack of variable declaration in `y = Date.now()` made y an implicit global, so it couldn't be optimised out no matter what. Even doing `let y = Date.now()` cuts out 400 lines.