TD;DR, Lazy Evaluation in Javascript object properties can be easily performed with Object.defineProperty(). Some time ago i was searching for a way to reach some kind of in . lazy evaluation node.js I was building an object that must have an attribute containing an instance of its same kind (recursively). This could be achieved through a function instead of an attribute, but this was a very specific scenario. So what i really needed was way to run this instantiation only when the object property was called, and not during the object construction. Even tough i found some Javascript articles about strategies on , nothing was working for what i was trying to do. call-by-name or call-by-need stackoverflow Most articles, code and libraries i came across was achieving through a . But this kind of object is not available in older versions of , which was my case. lazy evaluation Proxy node.js In brief, Lazy Evaluation, also known as call-by-need, is defined as: is a version of call by name. Call by need memoized But what is a call by name? And what about memoized? Call by Name & Call by Value First, lets examine these two functions: // Evaluates with call-by-name strategy1 function callByName (a, b) {2 if (a === 1) {3 return 104 }5 return a + b6 } // Evaluates with call-by-value strategy1 function callByValue (a, b) {2 if (a === 1) {3 return 104 }5 return a + b6 } Keep in mind that this is not Javascript. The first function evals through call-by-name, and the second one through call-by-value. To understand the difference between both types of evaluation strategies, we can look line by line of some pseudocode of how the execution might be executed. 1 > callByName (1, 2 + 3)2 > a === 13 > return 10 1 > callByValue(1, 2 + 3)1 > callByValue(1, 5)2 > a === 13 > return 10 You might seem familiar with the second one. This is because Javascript only passes arguments as values (or by reference if it is an object). In a function (or ) the numbers 2 and 3 are first summed up and then they are passed as function argument with the value 5. call-by-value Eager Evaluation On the other hand, in the function (or ) showed in the example, the second parameter is not even evaluated since the function returns 10 before is used by the function. call-by-name Lazy Evaluation b b Memoization In short, is an optimization technique to store pre-computed results (cache), to avoid recomputation for the same inputs. memoization In a more formal way, this means that if an idempotent function f(x) = g(x) + 1 will always output some value when it receives an input , then . y v f(v) = y So if is very in terms of time or machine resources, we can store the result in a cache to be returned when is the input, such that we don’t have to recompute again. g(v) expensive y v g(v) cache = {} cache[v] = y The Infinite Recursion Problem Now we will go through some very straightforward code of a recursive object which does lazy evaluation to avoid infinite recursion. We will analyse an infinite data structure called Stream, very common on Functional languages. Programming What would happen if we try to run this code? var stream = new Stream(10); > RangeError: Maximum call stack size exceeded As expected, it would fail with a . RangeError The problem is in the line of code below, where we are doing an of the attribute**.** Eager loading next this.next = new Stream(value + 1); Let’s change the Stream to be instead. Lazy Note that i also added a method to the Stream. Lets move on and try to run this code. takeUntil var stream = new Stream(10); console.log(stream.takeUntil(20)) > [10, 11, 12, 13, 14, 15, 16, 17, 18, 19] Now, not only we can build the Stream, but it is also possible to run algorithms like with this implementation. infinite takeUntil You may be thinking: Why we didn’t use here? memoization Well, we could, but this is no worthy since we are doing just a basic math operation of summing two operands. This is not expensive computationally like it would be, for example, to calculate if is a prime number. optimization value References: http://stackoverflow.com/questions/6605640/javascript-by-reference-vs-by-value#6605700 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty https://en.wikipedia.org/wiki/Evaluation_strategy https://en.wikipedia.org/wiki/Memoization is how hackers start their afternoons. We’re a part of the family. We are now and happy to opportunities. Hacker Noon @AMI accepting submissions discuss advertising &sponsorship To learn more, , , or simply, read our about page like/message us on Facebook tweet/DM @HackerNoon. If you enjoyed this story, we recommend reading our and . Until next time, don’t take the realities of the world for granted! latest tech stories trending tech stories