<p>Is it a deliberate design decision or a problem with our current day browsers which will be rectified in the coming versions?</p>

[{'answer_id': 39887, 'author': 'BlaM', 'author_id': 999, 'author_profile': 'https://Stackoverflow.com/users/999', 'pm_score': 0, 'selected': False, 'text': '<p>As far as I have heared Google Chrome will have multithreaded javascript, so it is a "current implementations" problem.</p>\n'}, {'answer_id': 39900, 'author': 'matt b', 'author_id': 4249, 'author_profile': 'https://Stackoverflow.com/users/4249', 'pm_score': 4, 'selected': False, 'text': "<p>Do you mean why doesn't the language support multithreading or why don't JavaScript engines in browsers support multithreading?</p>\n\n<p>The answer to the first question is that JavaScript in the browser is meant to be run in a sandbox and in a machine/OS-independent way, to add multithreading support would complicate the language and tie the language too closely to the OS.</p>\n"}, {'answer_id': 39913, 'author': 'Greg Roberts', 'author_id': 4269, 'author_profile': 'https://Stackoverflow.com/users/4269', 'pm_score': 1, 'selected': False, 'text': "<p>It's the implementations that doesn't support multi-threading. Currently Google Gears is providing a way to use some form of concurrency by executing external processes but that's about it.</p>\n\n<p>The new browser Google is supposed to release today (Google Chrome) executes some code in parallel by separating it in process.</p>\n\n<p>The core language, of course can have the same support as, say Java, but support for something like Erlang's concurrency is nowhere near the horizon.</p>\n"}, {'answer_id': 39922, 'author': 'pkaeding', 'author_id': 4257, 'author_profile': 'https://Stackoverflow.com/users/4257', 'pm_score': 4, 'selected': False, 'text': "<p>I don't know the rationale for this decision, but I know that you can simulate some of the benefits of multi-threaded programming using setTimeout.  You can give the illusion of multiple processes doing things at the same time, though in reality, everything happens in one thread.</p>\n\n<p>Just have your function do a little bit of work, and then call something like:</p>\n\n<pre><code>setTimeout(function () {\n    ... do the rest of the work...\n}, 0);\n</code></pre>\n\n<p>And any other things that need doing (like UI updates, animated images, etc) will happen when they get a chance.</p>\n"}, {'answer_id': 39931, 'author': 'Shog9', 'author_id': 811, 'author_profile': 'https://Stackoverflow.com/users/811', 'pm_score': 5, 'selected': False, 'text': '<p>Traditionally, JS was intended for short, quick-running pieces of code. If you had major calculations going on, you did it on a server - the idea of a JS+HTML <em>app</em> that ran in your browser for long periods of time doing non-trivial things was absurd.</p>\n\n<p>Of course, now we have that. But, it\'ll take a bit for browsers to catch up - most of them have been designed around a single-threaded model, and changing that is not easy. Google Gears side-steps a lot of potential problems by requiring that background execution is isolated - no changing the DOM (since that\'s not thread-safe), no accessing objects created by the main thread (ditto). While restrictive, this will likely be the most practical design for the near future, both because it simplifies the design of the browser, and because it reduces the risk involved in allowing inexperienced JS coders mess around with threads...</p>\n\n<p><a href="https://stackoverflow.com/questions/39879/why-doesnt-javascript-support-multithreading#40058">@marcio</a>:</p>\n\n<blockquote>\n  <p>Why is that a reason not to implement multi-threading in Javascript? Programmers can do whatever they want with the tools they have.</p>\n</blockquote>\n\n<p>So then, let\'s not give them tools that are so easy to <strong>misuse</strong> that every other website i open ends up crashing my browser. A naive implementation of this would bring you straight into the territory that caused MS so many headaches during IE7 development: add-on authors played fast and loose with the threading model, resulting in hidden bugs that became evident when object lifecycles changed on the primary thread. BAD. If you\'re writing multi-threaded ActiveX add-ons for IE, i guess it comes with the territory; doesn\'t mean it needs to go any further than that. </p>\n'}, {'answer_id': 39945, 'author': 'Grey Panther', 'author_id': 1265, 'author_profile': 'https://Stackoverflow.com/users/1265', 'pm_score': 3, 'selected': False, 'text': "<p>Just as matt b said, the question is not very clear. Assuming that you are asking about multithreading support in the language: because it isn't needed for 99.999% of the applications running in the browser currently. If you really need it, there are workarounds (like using window.setTimeout).</p>\n\n<p>In general multithreading is very, very, very, very, very, very hard (did I say that it is hard?) to get right, unless you put in extra restrictions (like using only immutable data).</p>\n"}, {'answer_id': 39961, 'author': 'Kamiel Wanrooij', 'author_id': 4174, 'author_profile': 'https://Stackoverflow.com/users/4174', 'pm_score': 9, 'selected': True, 'text': '<p>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.</p>\n\n<p>You can however use, as was suggested, <code>setTimeout</code> 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 <code>setTimeout</code> 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.</p>\n\n<p>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:</p>\n\n<ul>\n<li>Process A, Thread 1</li>\n<li>Process A, Thread 2</li>\n<li>Process B, Thread 1</li>\n<li>Process A, Thread 3</li>\n<li>Process A, Thread 4</li>\n<li>Process B, Thread 2</li>\n<li>Pause Process A</li>\n<li>Process B, Thread 3</li>\n<li>Process B, Thread 4</li>\n<li>Process B, Thread 5</li>\n<li>Start Process A</li>\n<li>Process A, Thread 5</li>\n</ul>\n\n<p>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.</p>\n\n<p>For the future of JavaScript, check this out: \n<a href="https://web.archive.org/web/20170920070053/https://developer.mozilla.org/presentations/xtech2006/javascript/" rel="noreferrer">https://developer.mozilla.org/presentations/xtech2006/javascript/</a></p>\n'}, {'answer_id': 2400168, 'author': 'Erich Kitzmueller', 'author_id': 65464, 'author_profile': 'https://Stackoverflow.com/users/65464', 'pm_score': 0, 'selected': False, 'text': "<p>Without proper language support for thread syncronization, it doesn't even make sense for new implementations to try. Existing complex JS apps (e.g. anything using ExtJS) would most likely crash unexpectedly, but without a <code>synchronized</code> keyword or something similar, it would also be very hard or even impossible to write new programs that behave correctly.</p>\n"}, {'answer_id': 2746824, 'author': 'Neil', 'author_id': 24315, 'author_profile': 'https://Stackoverflow.com/users/24315', 'pm_score': 5, 'selected': False, 'text': '<p>JavaScript multi-threading (with some limitations) is here. Google implemented workers for Gears, and workers are being included with HTML5. Most browsers have already added support for this feature.</p>\n\n<p>Thread-safety of data is guaranteed because all data communicated to/from the worker is serialized/copied.</p>\n\n<p>For more info, read:</p>\n\n<p><a href="http://www.whatwg.org/specs/web-workers/current-work/" rel="noreferrer">http://www.whatwg.org/specs/web-workers/current-work/</a></p>\n\n<p><a href="http://ejohn.org/blog/web-workers/" rel="noreferrer">http://ejohn.org/blog/web-workers/</a></p>\n'}, {'answer_id': 9967997, 'author': 'Rohit Reddy Korrapolu', 'author_id': 1249751, 'author_profile': 'https://Stackoverflow.com/users/1249751', 'pm_score': 2, 'selected': False, 'text': '<p>Intel has been doing some open-source research on multithreading in Javascript, it was showcased recently on GDC 2012. \nHere is the link for the <a href="http://www.youtube.com/watch?v=_adMEEAtDwE" rel="nofollow">video</a>. The research group used OpenCL which primarily focuses on Intel Chip sets and Windows OS. The project is code-named RiverTrail and the code is available on GitHub</p>\n\n<p>Some more useful links:</p>\n\n<p><a href="http://blogs.intel.com/research/2011/09/15/pjs/" rel="nofollow">Building a Computing Highway for Web Applications</a></p>\n'}, {'answer_id': 34133748, 'author': 'sergzach', 'author_id': 749288, 'author_profile': 'https://Stackoverflow.com/users/749288', 'pm_score': 2, 'selected': False, 'text': '<p>Currently some browsers do support multithreading. So, if you need that you could use specific libraries. For example, view the next materials:</p>\n\n<ul>\n<li><p><a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers" rel="nofollow">https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers</a>\n(support background threads);</p></li>\n<li><p><a href="https://keithwhor.github.io/multithread.js/" rel="nofollow">https://keithwhor.github.io/multithread.js/</a> (the library).</p></li>\n</ul>\n'}, {'answer_id': 39436742, 'author': 'OBDM', 'author_id': 6819297, 'author_profile': 'https://Stackoverflow.com/users/6819297', 'pm_score': -1, 'selected': False, 'text': '<p>Multi-threading with javascript is clearly possible using webworkers bring by HTML5.</p>\n\n<p>Main difference between webworkers and a standard multi-threading environment is memory resources are not shared with the main thread, a reference to an object is not visible from one thread to another. Threads communicate by exchanging messages, it is therefore possible to implement a synchronzization and concurrent method call algorithm following an event-driven design pattern.</p>\n\n<p>Many frameworks exists allowing to structure programmation between threads, among them OODK-JS, an OOP js framework supporting concurrent programming\n<a href="https://github.com/GOMServices/oodk-js-oop-for-js" rel="nofollow">https://github.com/GOMServices/oodk-js-oop-for-js</a></p>\n'}, {'answer_id': 42172751, 'author': 'Madhusoodan P', 'author_id': 4617334, 'author_profile': 'https://Stackoverflow.com/users/4617334', 'pm_score': -1, 'selected': False, 'text': '<p>However you can use eval function to bring concurrency TO SOME EXTENT</p>\n\n<pre><code>/* content of the threads to be run */\nvar threads = [\n        [\n            "document.write(\'Foo &lt;br/&gt;\');",\n            "document.write(\'Foo &lt;br/&gt;\');",\n            "document.write(\'Foo &lt;br/&gt;\');",\n            "document.write(\'Foo &lt;br/&gt;\');",\n            "document.write(\'Foo &lt;br/&gt;\');",\n            "document.write(\'Foo &lt;br/&gt;\');",\n            "document.write(\'Foo &lt;br/&gt;\');",\n            "document.write(\'Foo &lt;br/&gt;\');",\n            "document.write(\'Foo &lt;br/&gt;\');",\n            "document.write(\'Foo &lt;br/&gt;\');"\n        ],\n        [\n            "document.write(\'Bar &lt;br/&gt;\');",\n            "document.write(\'Bar &lt;br/&gt;\');",\n            "document.write(\'Bar &lt;br/&gt;\');",\n            "document.write(\'Bar &lt;br/&gt;\');",\n            "document.write(\'Bar &lt;br/&gt;\');",\n            "document.write(\'Bar &lt;br/&gt;\');",\n            "document.write(\'Bar &lt;br/&gt;\');",\n            "document.write(\'Bar &lt;br/&gt;\');",\n            "document.write(\'Bar &lt;br/&gt;\');"\n        ]\n    ];\n\nwindow.onload = function() {\n    var lines = 0, quantum = 3, max = 0;\n\n    /* get the longer thread length */\n    for(var i=0; i&lt;threads.length; i++) {\n        if(max &lt; threads[i].length) {\n            max = threads[i].length;\n        }\n    }\n\n    /* execute them */\n    while(lines &lt; max) {\n        for(var i=0; i&lt;threads.length; i++) {\n            for(var j = lines; j &lt; threads[i].length &amp;&amp; j &lt; (lines + quantum); j++) {\n                eval(threads[i][j]);\n            }\n        }\n        lines += quantum;\n    }\n}\n</code></pre>\n'}, {'answer_id': 52205680, 'author': 'Wilk', 'author_id': 916784, 'author_profile': 'https://Stackoverflow.com/users/916784', 'pm_score': 4, 'selected': False, 'text': '<p>Node.js 10.5+ supports <strong>worker threads</strong> as experimental feature (you can use it with <strong>--experimental-worker</strong> flag enabled): <a href="https://nodejs.org/api/worker_threads.html" rel="noreferrer">https://nodejs.org/api/worker_threads.html</a></p>\n\n<p>So, the rule is:</p>\n\n<ul>\n<li>if you need to do <strong>I/O bound ops</strong>, then use the internal mechanism (aka callback/promise/async-await)</li>\n<li>if you need to do <strong>CPU bound ops</strong>, then use worker threads.</li>\n</ul>\n\n<p>Worker threads are intended to be long-living threads, meaning you spawn a background thread and then you communicate with it via message passing.</p>\n\n<p>Otherwise, if you need to execute a heavy CPU load with an anonymous function, then you can go with <strong><a href="https://github.com/wilk/microjob" rel="noreferrer">https://github.com/wilk/microjob</a></strong>, a tiny library built around worker threads.</p>\n'}, {'answer_id': 59843875, 'author': 'Omar bakhsh', 'author_id': 11198747, 'author_profile': 'https://Stackoverflow.com/users/11198747', 'pm_score': 2, 'selected': False, 'text': "<p>Javascript is a single-threaded language. This means it has one call stack and one memory heap. As expected, it executes code in order and must finish executing a piece code before moving onto the next. It's synchronous, but at times that can be harmful. For example, if a function takes a while to execute or has to wait on something, it freezes everything up in the meanwhile.</p>\n"}, {'answer_id': 69784351, 'author': 'Ahmed Yasin Koculu', 'author_id': 673692, 'author_profile': 'https://Stackoverflow.com/users/673692', 'pm_score': 0, 'selected': False, 'text': '<p>Actually multi-threading is not related with the language itself.\nHere is a Multithreaded Javascript Engine for .NET.\nIt works pretty well with multi-threading scenarios.\nIt integrates with C# runtime so all the synchronization logic is similar to C#. You can start/await/wait tasks, and you can start threads. You can even put locks. Following sample demonstrates parallel loop using Javascript syntax in .NET runtime.</p>\n<p><a href="https://github.com/koculu/topaz" rel="nofollow noreferrer">https://github.com/koculu/topaz</a></p>\n<pre><code>var engine = new TopazEngine();\nengine.AddType(typeof(Console), &quot;Console&quot;);\ntopazEngine.AddType(typeof(Parallel), &quot;Parallel&quot;);\nengine.ExecuteScript(@&quot;\nvar sharedVariable = 0\nfunction f1(i) {\n    sharedVariable = i\n}\nParallel.For(0, 100000 , f1)\nConsole.WriteLine(`Final value: {sharedVariable}`);\n&quot;);\n</code></pre>\n<p>Besides that, Microsoft is working on Napa.js, a multi-threading supported Node.js clone.</p>\n<p><a href="https://github.com/microsoft/napajs" rel="nofollow noreferrer">https://github.com/microsoft/napajs</a></p>\n'}]
['https://Stackoverflow.com/questions/39879', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/184/']