Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is javascript multithreading possible with IFRAMEs

I'm currently playing with the idea of using IFRAMEs to implement a very simple multithreading engine. However my initial results are showing me that running in threads is slower than just running in a single thread.

My test is:

Single Thread

var start = new Date().getTime();
for (var i = 0; i < 300; i++) { /* Do costly processor operations */ }
debug('Took: ' + new Date().getTime() - start);

Multiple Threads

var start = new Date().getTime();
// In thread 1
for (var i = 0; i < 100; i++) { /* Do costly processor operations */ }
// In thread 2
for (var i = 100; i < 200; i++) { /* Do costly processor operations */ }
// In thread 3
for (var i = 200; i < 300; i++) { /* Do costly processor operations */ }
// In a callback in the original FRAME (thread)
debug('Took: ' + new Date().getTime() - start);

So as can be seen, I'm just splitting the work load amongst IFRAMEs (Note code above is only to give a better picture of what I am doing, it is not working code).

So I'm thinking that even using FRAMEs FireFox still has only one JS engine? Is this assumption correct? (rendering my research stupid), Are other browsers different?

Doing a quick googles I got this article: http://codediaries.blogspot.com/2009/12/real-javascript-multithreading-using.html

However the performance improvements achieved here are more than likely just doing parallel http requests rather than processing power.

Thanks for your insights.

Guido

like image 953
gatapia Avatar asked Dec 31 '09 02:12

gatapia


People also ask

Is JavaScript capable of multithreading?

Multithreading in JavaScript is the real right now. You can offload your work into multiple threads and share memory between them.

Can iFrames run same thread?

To sum up the other answers: No, iFrames usually run in the same thread/process as the main page.

How many types of threads are there in JavaScript?

js uses two kinds of threads: a main thread handled by the event loop and several auxiliary threads in the worker pool.

What is multithreading in MVC?

Multi-threading is a process that contains multiple threads within a single process. Here each thread performs different activities. For example, we have a class and this call contains two different methods, now using multithreading each method is executed by a separate thread.


1 Answers

Check out the HTML5 Web Workers Standard to see what JavaScript threading should look like. This is implemented in Firefox 3.5, Safari 4, and Chrome 3, but not IE. If you're willing to require a plugin for IE users and older browsers, check out Google Gears WorkerPool.

like image 173
Annie Avatar answered Oct 25 '22 15:10

Annie