performance - Do the most current JavaScript/ECMAScripte compilers optimize out unnecessary variable assignment when returning the value from a function call? -


say inside object implements file handling. want write code easier readability.

example of code can difficult tell return type, when there multiple nested function calls:

function create() {     return docreateaction(); } 

this example more readable introducing clarifying variable:

function create() {     var filehandle = docreateaction();     return filehandle; } 

in theory, second version perform identically because compiler has store result docreateaction() temporarily anyway (probably inside hiddenm, anonymous, short-lived temp variable). code slower when assigning named variable?

i either optimize variable out, or it's not worth bothering; , in either case have bigger fish fry. :-) there interesting aspect in relation tail calls.

but first, in terms of simple performance: empirically, this simplistic, synthetic test suggests performance of function doesn't vary depending on whether there's variable. note minifier remove variable before javascript engine gets in, if use decent minifier.

moving on tail-calls: may know, of es2015 in strict mode specificaton requires tail-call optimization, means when function returns result of calling function b, rather having b return result returns caller, passes control directly b returns result caller. more efficient in several ways (avoids creating frame on stack, avoids jump).

javascript engines still working on tco implementation. (follow v8's progress here, instance.) if read spec correctly (no mean feat), first example has docreateaction in tail position , can optimized via tco, second not. , tco in v8 in node 8.2.1 (v5.8.283.41) indeed optimize first not second.

so there implications in regard, depending on whether once engines @ point of having implemented tco specified, go beyond spec cases is, in effect, tail call.


i used strict using variable in situation debugging purposes; moderately-recent versions of chrome's devtools make unnecessary that purpose (and of course, minifier remove anyway): if step return, see return value in local scope list of variables. of course, that's useful if you're using chrome's devtools (firefox's, instance, don't [yet?]).


Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -