Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8381069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:36:56+00:00 2026-06-09T16:36:56+00:00

I am currently writing a little library in JavaScript to help me delegate to

  • 0

I am currently writing a little library in JavaScript to help me delegate to a web-worker some heavy computation .

For some reasons (mainly for the ability to debug in the UI thread and then run the same code in a worker) I’d like to detect if the script is currently running in a worker or in the UI thread.

I’m not a seasoned JavaScript developper and I would like to ensure that the following function will reliably detect if I’m in a worker or not :

function testenv() {
    try{
        if (importScripts) {
            postMessage("I think I'm in a worker actually.");
        }
    } catch (e) {
        if (e instanceof ReferenceError) {
            console.log("I'm the UI thread.");
        } else {
            throw e;
        }
    }
}

So, does it ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:36:57+00:00Added an answer on June 9, 2026 at 4:36 pm

    As noted there is an answer in another thread which says to check for the presence of a document object on the window. I wanted to however make a modification to your code to avoid doing a try/catch block which slows execution of JS in Chrome and likely in other browsers as well.

    EDIT: I made an error previously in assuming there was a window object in the global scope. I usually add

    //This is likely SharedWorkerContext or DedicatedWorkerContext
    window=this;
    

    to the top of my worker loader script this allows all functions that use window feature detection to not blow up. Then you may use the function below.

    function testEnv() {
      if (window.document === undefined) {
        postMessage("I'm fairly confident I'm a webworker");
      } else {
        console.log("I'm fairly confident I'm in the renderer thread");
      }
    }
    

    Alternatively without the window assignment as long as its at top level scope.

    var self = this;
    function() {
      if(self.document === undefined) {
        postMessage("I'm fairly confident I'm a webworker");
      } else {
        console.log("I'm fairly confident I'm in the renderer thread");
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently writing a little C# library to simplify implementing little physical simulations/experiments. The
I'm currently writing a little django app to get some practice with the framework.
I'm currently writing a little GUI program that does some work and exits afterwards.
currently I'm writing little program that reads elf file header and prints some information
I am currently writing user interface in ember.js and need some help in server-side
I'm currently writing myself a little C# back up program. I'm using a standard
I'm currently writing several Python modules which perform some I/O. Thoses modules can be
I'm currently writing some update polling stuff. I try to avoid writing even a
I'm currently in the process of writing a little blog / generic posting system
I'm currently writing a little zsh function that checks all of my git repositories

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.