Run these with time WHICH=exN node run.js
, where N
is 1, 2, or 3.
For the first run, compare GC:
$ time WHICH=ex1 node --trace_gc run.js
$ time WHICH=ex2 node --trace_gc run.js
$ time WHICH=ex3 node --trace_gc run.js
Note that the "fast" examples don't more than twice-ish. Weird. Why would the closure one GC so much more that the others? The only garbage being generated is (potentially) boxed doubles + whatever it has to allocate for call itself.
...unless after a certain point, it doesn't need to allocate for the call, because the call has been inlined.
$ time WHICH=ex1 node --trace_inlining --trace_osr run.js
$ time WHICH=ex2 node --trace_inlining --trace_osr run.js
$ time WHICH=ex3 node --trace_inlining --trace_osr run.js
ex1's distance
function can't be inlined! here's the code in v8 that's preventing it..
Were the loop in the same context, it could inline distance entirely, but since it's not, we have to keep the function call around and its associated garbage generation.