-1

I don't really understand how V8 handle that kind of code,

Does it allocate heap memory for variable "a"?

Does it use mutexes to protect the variable?

Can someone shed some light on the inner workings of the V8 in this example?

Thank you in advance

let f, g;
function scope() {
    let a = 1;
    f = async function() {
        a = 2;
    }
    g = function() {
        return a;
    }
};
scope();
f().then( _ => console.log(g()) ); // print 2
Chr
  • 175
  • 10
  • [JavaScript is single threaded](https://stackoverflow.com/questions/21718774/how-is-javascript-single-threaded). You don't need mutexes. – jabaa Mar 14 '21 at 22:24
  • Ok, so how async functions work ? – Chr Mar 14 '21 at 22:25
  • async functions run concurrently with [time slicing](https://stackoverflow.com/questions/12332429/how-does-time-slicing-happen-in-javascript). – jabaa Mar 14 '21 at 22:26
  • Ok thank you for your help. So every X instructions javascript will check a queue or something to see if there is async functions to execute concurrently ? – Chr Mar 14 '21 at 22:30
  • [JavaScript Event Loop](https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop) [Node.js Event Loop](https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/) – jabaa Mar 14 '21 at 22:30

2 Answers2

1

(V8 developer here.)

Does it allocate heap memory for variable "a"?

Yes. As V8 calls it, a will be "context-allocated", so that closures created in scope (such as f and g) can access it (via their "context"). This has nothing to do with async functions: if you only defined g in your example, that would still happen.

Also, this is something of an implementation detail. If you were to implement an as-simple-as-possible JavaScript engine, you'd simply heap-allocate all variables. Modern high-performance JS engines try to stack-allocate variables when they can, which is what leads to this distinction; but that's just an optimization, and doesn't change any observable behavior.

Does it use mutexes to protect the variable?

It doesn't need to. An async function doesn't run concurrently...

so how async functions work ?

...see e.g. here for a very detailed description: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

In short, async/await is mostly just syntactic sugar, allowing you avoid "callback hell" and instead write code that looks straight-line (but gets split into chunks under the hood). It does not add concurrency to JavaScript.

every X instructions javascript will check a queue or something to see if there is async functions to execute concurrently ?

No (as that would effectively be concurrency). When control returns to the event loop (i.e. all active functions have returned), then the next waiting callback is called.

jmrk
  • 19,523
  • 5
  • 30
  • 43
0

Does it allocate heap memory for variable "a"?

I don't think we should think about it. Js virtual mashine is quite complex.

Does it use mutexes to protect the variable?

Js is single-threaded. No one promise/async will invoke until current sync func ends. It's difficult to stop think about such details after using languages like C++, but js is really simple.

A Ralkov
  • 1,028
  • 7
  • 17