javascript - Where exactly does the performance advantage of LazyEvalutaion emerge from? -


in past few days, have researched lazyevaluation, in performance aspect mostly, wondering from performance advantage of lazyevalutaion emerges.

it's been unclear me reading various articles few including

what advantages of lazy evaluation?

this refers the evaluation of syntax tree. if evaluate syntax tree lazily (i.e. when value represents needed), must carry through preceeding steps of computation in entirety. overhead of lazy evaluation. however, there 2 advantages. 1) you not evaulate tree unnecessarily if result never used,

javascript, instance, implemented if syntax.

if(true) {     //to evaluate } else {     //not evaluate } 

in ordinary scenario, don't have performance issue.

to evaluated done, not evaluated ignored in syntax tree.

however, in recursive loop, instance, tak function aka tarai function

function tak(x,y,z){ return (x <= y) ? y : tak(tak(x-1, y, z), tak(y-1, z, x), tak(z-1, x, y)); }

since eager evaluation strategy of js evaluates function(arguments) inevitability, if - else - not evaluated control no longer works, , number of evaluation step of tak function explodes.

against disadvantage of eager evaluation(of js or other languages), haskell can evaluate tak without problems is, , js library such lazy.js outperforms in specific area functional programming recursive list management required.

aside infinite list, understand exact reason performance advantage of lazyevaluation. correct?

i think you've got right idea.

i don't think need complexity though. imagine javascript was

if (veryexpensivefunction()) {   dothis(); } else {   dothat(); } 

now imagine veryexpensivefunction implemented.

function veryexpensivefunction() {    return true || evenmoreexpensivefunction(); } 

if javascript because || lazy (only evaluates second argument if needed) return (because true is, well, true!). if implemented instead like

function veryexpensivefunction() {   var = true;   var b = evenmoreexpensivefunction(); // forces evaluation    return || b; } 

this take ages evaluate because you've unnecessarily evaluated arguments. imagine magic applied || applied every function (i.e. lazy language) , can imagine sort of performance advantages laziness might bring.

in example

function tak(x,y,z){ return (x <= y) ? y : tak(tak(x-1, y, z), tak(y-1, z, x), tak(z-1, x, y)); } 

let's imagine lazy.

 var = tak(1,2,3); 

this doesn't (nothing needs evaluated). a isn't used, nothing evaluated.

 var = tak(1,2,3);  console.log(a); 

now we're in position drive evaluation of tak. evaluating need results, first substitute values in (replace variables arguments). evaluate condition, , side of conditional need.

 console.log((1 <= 2) ? 2 : tak(tak(1-1, 2, 3), tak(2-1, 1, 3), tak(3-1, 1, 2));  console.log(   true  ? 2 : tak(tak(1-1, 2, 3), tak(2-1, 1, 3), tak(3-1, 1, 2));  console.log(2); 

in example (assuming i've not made horrible typos), don't need evaluate else other arguments , no recursive calls made.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -