Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

290
Views
Can console.log()s called from two different Worker threads ever step on each other, or are they always one after the other?

I have this small piece of example code:

//main.js

const {Worker} = require("worker_threads");

const worker = new Worker("./background.js", {workerData: {}});

function loop() {

    console.log("main: 1234567890");
    setTimeout(loop, 0)

}

loop();

And:

//background.js

const {parentPort, workerData} = require("worker_threads");

function loop () {

    console.log("background: 1234567890");
    setTimeout(loop, 0)

}

loop();

Run like so: node main.js

The output is this:

background: 1234567890
main: 1234567890
background: 1234567890
main: 1234567890
background: 1234567890
background: 1234567890
#etc

Sometimes several console.log()'s from one thread are called before any are called by the other. This is expected, and fine.

But is there any chance that a console.log() could be called in the middle of another console.log()? So for example, could this happen?

background: 123main: 12345456786789090

I haven't observed it in my output, but I'd like to know from a canonical source that Workers/console.log() do not work that way in node.js.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

console.log() for primitives (non-objects) is thread-safe. So, in your particular example where you're passing a string, that is safe and you won't get mixed up output.

But, if you pass an object to console.log(obj) and threads are in the process of modifying that object, then there can be issues before the object is turned into output and marshalled over to the logging output. This can even happen with just a single thread where you immediately modify the object on subsequent lines of code.

This apparently occurs because the conversion of the object to the console output is "lazy" and not immediate. This is probably an attempt to run this in parallel in order to minimize the performance impact of calls to console.log(). I've seen this in real code and the work-around was to call console.log(JSON.stringify(obj)) rather than console.log(obj) to remove any chance of an issue with the object being modified before it got lazily logged.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!