Is it a deliberate design decision or a problem with our current day browsers which will be rectified in the coming versions?
JavaScript does not support multi-threading because the JavaScript interpreter in the browser is a single thread (AFAIK). Even Google Chrome will not let a single web page’s JavaScript run concurrently because this would cause massive concurrency issues in existing web pages. All Chrome does is separate multiple components (different tabs, plug-ins, etcetera) into separate processes, but I can’t imagine a single page having more than one JavaScript thread.
You can however use, as was suggested, setTimeout
to allow some sort of scheduling and “fake” concurrency. This causes the browser to regain control of the rendering thread, and start the JavaScript code supplied to setTimeout
after the given number of milliseconds. This is very useful if you want to allow the viewport (what you see) to refresh while performing operations on it. Just looping through e.g. coordinates and updating an element accordingly will just let you see the start and end positions, and nothing in between.
We use an abstraction library in JavaScript that allows us to create processes and threads which are all managed by the same JavaScript interpreter. This allows us to run actions in the following manner:
This allows some form of scheduling and fakes parallelism, starting and stopping of threads, etcetera, but it will not be true multi-threading. I don’t think it will ever be implemented in the language itself, since true multi-threading is only useful if the browser can run a single page multi-threaded (or even more than one core), and the difficulties there are way larger than the extra possibilities.
For the future of JavaScript, check this out: https://developer.mozilla.org/presentations/xtech2006/javascript/
I think never implemented is too narrow a vision. I guarantee web apps will eventually be able to be truly multithreaded (it's only logical, as web apps become more dominant, and hardware becomes more parallel), and as I see it, since JavaScript is the de-facto language of web development, it will eventually have to support multithreading or be replaced by something that does.
Never is probably a bit of a too bold statement :) but I still think the advantages of true multithreaded javascript are not feasible in the foreseeable future ;)
Although I would argue that web workers are more concurrent through a process-model than a thread-model. Web workers use message passing as the means of communication, which is an elegant solution to the 'usual' concurrency issues in multithreaded applications. I am not sure if they can actually operate on the same objects as the main page concurrently. They cannot access the DOM as far as I know. Most of this is semantics though, web workers look promising for all intents and purposes.
difficulties there are way larger than the extra possibilities I'm not sure if you think of all the extra possibilities. I'm especially thinking of cases where webgl is used like in games or graphical visualizations. E.g. consider the new 3D version of Google Maps. In cities with many 3D models my PC needs ~2 min to load everything when many houses have to be rendered. At certain angles neither my graphics card nor my network are working to capacity. But 1 of 8 processors is at 100%. Multithreading is also a big concern in terms of fps as this example shows: youtube.com/watch?v=sJ2p982cZFc