Im not kidding, your code might not actually be slow, take out your console logs and never benchmark with them inside the code exec, its staggering...
For further actions, you may consider blocking this person and/or reporting abuse
The real reason behind this is that console.log is actually synchronous and mixing sync with async is pretty bad in any language or runtime. People should use something like opentelemetry-js or other telemetry tools for logging instead of console.logs.
Could be but if you move a console.log into an async function theoretically you wonโt be blocking and you wonโt get the slow down correct? I think thereโs more at work here related to the DOM which others have mentioned but equally it might well be this, either way Iโm interested in the problem ๐
Oh no, massive misunderstanding. Async functions do not make anything asynchronous. JavaScript is still single threaded. The only thing they do is making the syntax around using promises or callbacks easier. But a synchronous function like console.log (see, it has no promise) doesn't suddenly turn into an asynchronous one by wrapping it in async. I really suggest you read up on how async/await works because you might be making a lot of mistakes in your code.
Whether JavaScript is single threaded depends on the runtime. JS in the browser is single threaded, but NodeJS can be multi threaded. The async keyword does infact mean your function is wrapped in a promise. developer.mozilla.org/en-US/docs/W...
Iโm sorry Iโm using slang for asynchronous not referring to async await
Furthermore Iโve been a JavaScript developer for 10 years I understand the call stack and event loop ๐ญ๐ (thanks for the giggle)
Because console in chrome is DOM and every line is a new element, so console.log is roughly equivalent to
Makes sense. I/O operations are slow (even if it's just output on the screen.)
Damn sure enough, it's pretty slow.
10000 rows.
with console
422.90000000596046
410.90000000596046
437.30000001192093
without
0.30000001192092896
0.4000000059604645
0.10000002384185791
These results was created without opened dev tools tab ?
thats an interesting question, but if a log is logged in the forrest and nobody is around to see it :) I honestly dont know.
Displaying a console.log in a terminal is super slow period.
Try to reduce the amount of data that is stored in the console.log, also use session store instead of file.
with the definition set as needed here
EXAMPLE:
// Save element position
app.post('/position', (req, res) => {
const positionData = req.body;
const elementId = positionData.elementId;
const sessionData = req.session;
// Update the session data with the new position data for the element
sessionData[elementId] = positionData;
// Return a success status code
res.sendStatus(200);
});
server.log? im refering to JavaScript console.log in both browser and backend environments, particularly in memory and chromium
U can also use session store with redis.
look on my files how I solved faster response.
now i'm curious. What numbers did you see?
How did you get it ?
You got me curious. Show us plz
This is generated by chatgpt, isn't it?
No ๐ I just write on my phone, what makes you say that?
Oh did you mean the comment sorry I was out of context
This is intentional, the Node.js docs say:
A note on process I/O#
process.stdout and process.stderr differ from other Node.js streams in important ways:
They are used internally by console.log() and console.error(), respectively.
Writes may be synchronous depending on what the stream is connected to and whether the system is Windows or POSIX:
Files: synchronous on Windows and POSIX
TTYs (Terminals): asynchronous on Windows, synchronous on POSIX
Pipes (and sockets): synchronous on Windows, asynchronous on POSIX
These behaviors are partly for historical reasons, as changing them would create backward incompatibility, but they are also expected by some users.
Synchronous writes avoid problems such as output written with console.log() or console.error() being unexpectedly interleaved, or not written at all if process.exit() is called before an asynchronous write completes. See process.exit() for more information.
Might be good to show some examples... stackoverflow.com/a/26754011/21061 gives a nice breakdown with evidence...
Thanks youโve all been helpful providing evidence and tests I didnโt expect this post to blow up so much ๐ฎ I see the point about evidence and agree but I just wrote this while making breakfastโฆ.
Checkout this
How a single line code change, improved my performance
MR.H ใป Feb 26 ใป 2 min read
It's like cout in C++. You shouldn't really use both cout and console.log if you're looking for performance in the console.
You're causing a write operation, which is inherently blocking. Buffer your output or don't have any at all when it's performance critical.