Posts

Showing posts from November, 2014

Understanding Code Execution in Javascript

If you are doing any form of programming in Javascript then understanding its eventloop is very important. Any javascript engine has three kinds of memory models: Stack, which has the current function pointer an gets executed sequentially. Heap, this stores all the objects, functions basically anything that is initiated is stored here. Queue, all things to be executed is stored here and stack picks up tasks to do from the queue. So, to understand this further, when a function is getting executed its loaded in the stack and if it encounters any setTimeouts then it would be added in the queue and when the current stack gets empty then the new function to be executed from the queue. This code run will explain the process: console.log("Add this code to queue"); setTimeout(function() {                                                //This goes and sits in the queue.                            console.log("Running next event from the queue.");