by chime · 9y ago · 0 comments
(function() {
function fib(x) {
y = Date.now(); // the useless line I added
return x <= 1 ? x : fib(x - 1) + fib(x - 2);
}
let x = Date.now();
if (x * 2 > 42) x = fib(10);
global.result = x;
})();
I understand Date might not be acceptable for inner loops but a lot of my code that deals with scheduling would benefit significantly if I could precompute some of the core values/arrays using a tool like prepack.If you replaced the line with say: `var y = 4;` you'll notice that it has been optimized out.
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.
(function() {
for(var i = 0; i < 10000; ++i){
Date.now();
}
})();It makes no sense to use an optimization tool that does the exact opposite in cases it cannot handle. In no world is 1400 lines better than 10.
If you know the possible range of accepted inputs, 1400 loc would be better than 10.
One normally think much more loc is worst than a one line solution, but that's, like most answers, always depends.
And that's a simple case. There are lot of similar cases one dont realize.