Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "call stack" and "task queue"

I'm a developer having difficulties in differentiating between the terms Call Stack and Task Queue. Is there anyone who can help me explain the difference?

Thanks in advance.

like image 639
Saurabh Singh Mehra Avatar asked Nov 23 '15 15:11

Saurabh Singh Mehra


People also ask

What is in a call stack?

A call stack is a mechanism for an interpreter (like the JavaScript interpreter in a web browser) to keep track of its place in a script that calls multiple functions — what function is currently being run and what functions are called from within that function, etc.

Is call stack and stack same?

Often people use these somewhat interchangeably but a call stack is solely a data structure. It describes a stack of function calls along with various associated state like the values of local variables, return states, etc. The stack is also a data structure, but it's ultimately a memory allocator.

Is call stack LIFO or FIFO?

At the most basic level, a call stack is a data structure that uses the Last In, First Out (LIFO) principle to temporarily store and manage function invocation (call).

Is call stack a FIFO?

A stack is, by definition, last in first out (LIFO). A first in first out (FIFO) data structure would be a queue, not a stack. Show activity on this post. The callstack gets filled from bottom to top: first f1() gets added (this function is being executed), when a subfunction ( f1.


1 Answers

eg.

in JavaScript there is function called timeout. when you call the function timeout on the "call stack" it would register in "job queue". It won't fire immediately, but will be triggered once the time is reached.

timeout(function(){
    console.log("one");
}, 100);

console.log("two");

in call stack, console.log("one") is triggered first, but in job queue, the result will display after two.

like image 76
Weijing Jay Lin Avatar answered Nov 14 '22 22:11

Weijing Jay Lin