Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

console.log() async or sync?

I am currently reading Async Javascript by Trevor Burnham. This has been a great book so far.

He talks about this snippet and console.log being 'async' in the Safari and Chrome console. Unfortunately I can't replicate this. Here is the code:

var obj = {};  console.log(obj);  obj.foo = 'bar'; // my outcome: Object{}; 'bar'; // The book outcome: {foo:bar}; 

If this was async, I would anticipate the outcome to be the books outcome. console.log() is put in the event queue until all code is executed, then it is ran and it would have the bar property.

It appears though it is running synchronously.

Am I running this code wrong? Is console.log actually async?

like image 582
HelloWorld Avatar asked Apr 30 '14 15:04

HelloWorld


People also ask

Is console log sync or async?

"So what?" you probably asked? - This is important part, because console. log() is asynchronous. All asynchronous functions come to Event Table in terms of Event Loop.

Is console log async Nodejs?

Console logging is asynchronous. If your code does a lot of logging, then it can generate a lot of superfluous asynchronous operations.

What does console log () do?

The console. log() is a function in JavaScript which is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user.

Does console log slow down performance?

Yes, it will reduce the speed, though only negligibly. But, don't use it as it's too easy for a person to read your logs.


2 Answers

console.log is not standardized, so the behavior is rather undefined, and can be changed easily from release to release of the developer tools. Your book is likely to be outdated, as might my answer soon.

To our code, it does not make any difference whether console.log is async or not, it does not provide any kind of callback or so; and the values you pass are always referenced and computed at the time you call the function.

We don't really know what happens then (OK, we could, since Firebug, Chrome Devtools and Opera Dragonfly are all open source). The console will need to store the logged values somewhere, and it will display them on the screen. The rendering will happen asynchronously for sure (being throttled to rate-limit updates), as will future interactions with the logged objects in the console (like expanding object properties).

So the console might either clone (serialize) the mutable objects that you did log, or it will store references to them. The first one doesn't work well with deep/large objects. Also, at least the initial rendering in the console will probably show the "current" state of the object, i.e. the one when it got logged - in your example you see Object {}.

However, when you expand the object to inspect its properties further, it is likely that the console will have only stored a reference to your object and its properties, and displaying them now will then show their current (already mutated) state. If you click on the +, you should be able to see the bar property in your example.

Here's a screenshot that was posted in the bug report to explain their "fix":

So, some values might be referenced long after they have been logged, and the evaluation of these is rather lazy ("when needed"). The most famous example of this discrepancy is handled in the question Is Chrome's JavaScript console lazy about evaluating arrays?

A workaround is to make sure to log serialized snapshots of your objects always, e.g. by doing console.log(JSON.stringify(obj)). This will work for non-circular and rather small objects only, though. See also How can I change the default behavior of console.log in Safari?.

The better solution is to use breakpoints for debugging, where the execution completely stops and you can inspect the current values at each point. Use logging only with serialisable and immutable data.

like image 155
Bergi Avatar answered Sep 22 '22 22:09

Bergi


This isn't really an answer to the question, but it might be handy to someone who stumbled on this post, and it was too long to put in a comment:

window.console.logSync = (...args) => {   try {     args = args.map((arg) => JSON.parse(JSON.stringify(arg)));     console.log(...args);   } catch (error) {     console.log('Error trying to console.logSync()', ...args);   } }; 

This creates a pseudo-synchronous version of console.log, but with the same caveats as mentioned in the accepted answer.

Since it seems like, at the moment, most browsers' console.log's are asynchronous in some manner, you may want to use a function like this in certain scenarios.

like image 30
V. Rubinetti Avatar answered Sep 21 '22 22:09

V. Rubinetti