Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I know, I know -- it isn't possible presently but my question is this:

Would it be possible to write a browser plug in for the major browsers that enables true, multi-threaded JavaScript?

It just seems like this would be possible and, if so, it would take the web (not to mention JavaScript) to new heights.

share|improve this question

closed as too broad by gnat, Ixrec, Adam Zuckerman, Snowman, Eric King Apr 21 at 16:52

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

up vote 8 down vote accepted

The DOM and it's various APIs are NOT thread-safe. No browser plug-in will be able to make them thread-safe without essentially breaking the pure multi-threading.

Some DOM functions like document.getElementsByTagName() are inherently thread-unsafe because they return a dynamic collection that automatically changes as the DOM is changed. You couldn't even safely loop through the results of that collection if some other thread might be changing the DOM at the same time. A new DOM API could be written with thread safety in mind, but that would be a major undertaking and some existing behaviors would have to be discontinued.

Plus, modern browsers already have web workers which allow pure javascript multi-threading (without DOM access in the threads) so you'd have to describe what more beyond web workers that doesn't require DOM access you are interested in implementing.

share|improve this answer

Brendan Eich has voiced his opposition to multi-threading in JavaScript several times:

So my default answer to questions such as the one I got at last May’s Ajax Experience, “When will you add threads to JavaScript?” is: “over your dead body!” (source)

And he goes on to list a few reasons why.

It's not just the DOM that's not thread-safe, the language itself does not lend itself to graceful multithreading. One of the big problems facing JavaScript is the highly flexible and dynamic type system. Since almost everything can be modified and redefined at run time, what happens to when the programmer modifies the body of Object.prototype.toString method while another thread is actually calling Object.prototype.toString? Or more generally, any method? The only safe way to handle this would be to synchronize any operations modifying the any method on any object -- since this a fairly common task in JavaScript, you would essentially revert it back to a single-threaded language again.

Web workers are able to get around this issue by ensuring that that the worker scripts do not share the same memory space as the main thread, so modifying objects in the work cannot interrupt or corrupt the operation of the main thread.

share|improve this answer

protected by gnat Jun 25 '15 at 16:34

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.