views:

128

answers:

4

Hi All,

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

A: 

No, Javascript generally does not support multi-threading. Most interpreters do not have any multi-threading capabilities built-in (just like PHP), probably for portability reasons.

However, since the Rhino engine is written entirely in Java, you may be able to tap into the Thread class, but this would only be feasible if you are doing server-side Javascript.

Josh Stodola
Yeah, I'm aware of the setTimeout approaches, however that just queues work in the single thread so no good. I was just hoping that iframes would use their own interpreter (engine).
gatapia
No, it does not work that way. It is still just one browser instance. You would have to launch a new window, and I am not even sure if that would work either.
Josh Stodola
Worth a try though. Launch a few windows, make them small as possible and move them to the corner. Use `opener` to access the underlying collection. Im on an iPhone otherwise I'd do it LOL. Watching the Husker game.
Josh Stodola
Yeah, I was thinking of a window.open approach, the only problem with this is that you would have all these windows popping up when you needed a thread (that is if it works). You can hide iframes but I don;t think you can hide windows (they would at least appear on your task bar or as empty tabs).
gatapia
-1 "Interpreted" has nothing to do with threading.
Jonathan Feinberg
Regardless, you are not going to get away with it unless you "trick the system". What's interesting is that the Ruby language actually has multi-threading capabilities built into its interpreter (as opposed to OS wrappers), so you would think it is conceivable with Javascript, but no interpreters that I know of support it at this point.
Josh Stodola
If you only care about Firefox, you may be able to tap into the Java Thread class, since the Rhino interpreter is written in Java: http://www.mozilla.org/rhino/ScriptingJava.html
Josh Stodola
Conversely, if you're looking to make this work on any browser, you should re-run your test on Google Chrome since its multi-process architecture may mean that JavaScript in different frames runs in different processes.
Annie
Josh: Firefox uses Spidermonkey, not Rhino.
Ken
@Jonathan You're right. I changed my answer.
Josh Stodola
@Ken True (and soon to be called TraceMonkey), but they are essentially brother and sister.
Josh Stodola
@Ken, @Josh, it's true firefox uses spidermonkey, not rhino, but spidermonkey has access to liveconnect, which provides a rhino-like bridge to java from javascript in a browser. I do not believe it's possible though still, since threading in JAVA requires the ability to subclass/implement a runnable thread class interface, which can only be done from javascript in rhino. However, you could run rhino in a java applet, and multithread javascripts inside the applet.
Breton
A: 

You could try wrapping your operations in a setTimeout call.

Jonathan S.
`window.setTimeout` doesn't create a new thread.
Tim Down
+3  A: 

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.

Annie
It's available today, in 3 of the 4 most popular browsers (firefox, safari, chrome)
Breton
Thanks Breton! I updated my post.
Annie
+1 for introducing me to Web Workers! This is going to save me some serious time :) thx Annie
Mike
A: 

Guys,

I've decided on a browser dependant solution. Basically I will use Workers if available, then Gears if available. Finally just single threaded.

Thanks all

Guido

gatapia
You should probably just select Annie's answer as "accepted" then (click the large checkmark to the right of her answer, under the voting buttons). That will tell people that her answer pretty much meets your needs.
Brian Campbell