16

Is it possible to run JavaScript code in parallel in the browser? I'm willing to sacrifice some browser support (IE, Opera, anything else) to gain some edge here.

3
  • 1
    Er, if you are willing to sacrifice compatibility, why can't you implement you program in something that executes faster than JavaScript and run that? Commented Oct 15, 2010 at 16:51
  • 1
    @Ira: Do you have something in mind? Commented Oct 15, 2010 at 17:38
  • I started to respond to your comment, and decided to make it an answer. Commented Oct 15, 2010 at 18:00

5 Answers 5

14

If you don't have to manipulate the dom, you could use webworkers ... there's a few other restrictions but check it out @ http://ejohn.org/blog/web-workers/

Sign up to request clarification or add additional context in comments.

4 Comments

Hmmm. Looks perfect though I'm interacting with a canvas element. Probably won't work with that, am I correct?
I believe updating a canvas node is the same as interacting with the DOM. But it never hurts to test.
You can use your web-workers to send control messages that are then interpreted to change the DOM, there's just no direct access.
Webworks forks by executing new Worker("worker.js");, which loads the new script and runs it. That's a pretty high overhead fork. The computation better be a lot of work to make this worth it. (The examples given on the link are things like "ray tracing", which are a lot of work).
8

Parallel.js of parallel.js.org (see also github source) is a single file JS library that has a nice API for multithreaded processing in JavaScript. It runs both in web browsers and in Node.js.

1 Comment

The original link is broken. Is this the same as parallel.js.org?
3

Perhaps it would be better to recode your JavaScript in something that generally runs faster, rather than trying to speed up the Javascript by going parallel. (I expect you'll find the cost of forking parallel JavaScript activities is pretty high, too, and that may well wipe out any possible parallel gain; this is common problem with parallel programming).

Javascript is interpreted in most browsers IIRC, and it is dynamic on top of it which means it, well, runs slowly.

I'm under the impression you can write Java code and run it under browser plugins. Java is type safe and JIT compiles to machine code. I'd expect that any big computation done in Javascript would run a lot faster in Java. I'm not specifically suggesting Java; any compiled language for which you can get a plug in would do.

As an alternative, Google provides Closure, a JavaScript compiler. It is claimed to be a compiler, but looks like an optimizer to me and I don't know much it "optimizes". But, perhaps you can use that. I'd expect the Closure compiler to be built into Chrome (but I don't know for a fact) and maybe just running Chrome would get your JavaScript compiler "for free".

EDIT: After reading about what about Closure does, as compiler guy I'm not much impressed. It looks like much of the emphasis is on reducing code size which minimizes download time but not necessarily performance. The one good thing they do in function inlining. I doubt that will help as much as switching to a truly compiled langauge.

EDIT2: Apparantly the "Closure" compiler is different than the engine than runs JavaScript in Chrome. I'm told, but don't know this for a fact, that the Chrome engine has a real compiler.

8 Comments

It compiles JavaScript into JavaScript, optimizing along the way (inlining functions, etc.). See the FAQs. I'm concerned that you're making huge assumptions about how JavaScript performance compares to Java — for instance, WebKit's JavaScript engine uses a JIT compiler — and whether Java is appropriate for the OP's use case.
Java does not run faster in a browser then JavaScript. Not only is it slow and clunky to code in (for a web application) but you are severely limiting the interaction with the DOM unless the entire page is an applet. Any big computation could be done in Java, however I don't have any really big computations. I would like to parallel an operation on a pet project I am working on the goal of which is to have no need for a browser plugin or other external lib.
@Sidnicious: yes, I figured out it was JavaScript -> JavaScript, which I why I called it an "optimizer". I did suggest that a real compiler existed (perhaps under Chrome); glad to hear there's another from WebKit. Having said that, Java's data model and accesses IMHO are closer to real machines than JavaScript and I'd be pretty surprised if effectively identical computations compiled by Java JITs with Sun's/IBM's team ran slower than Webkit's or any other "compiled" JavaScript. OP didn't specify what he was doing, only that it was slow, and one assumes as a default that is computation.
@Josh K: If you don't have any really big computations, you're going to have a hard time running them in parallel, as forking overhead may dominate the parallel execution time. I understand DOM access might be slower from Java (er, the browser doesn't offer a DOM interface to Java?); if you want parallel access to the DOM you may have a whole new set of problems related to data races and atomicity. Since you didn't specify what you were doing in advance, what you got was generic advice.
@Josh K: ... cute project at your link ... I still don't know what you are you are doing that requires parallelism here, since this appears to be something that is designed to operate at people speeds.
|
2

Intel is coming up with an open-source project codenamed River Trail check out http://www.theregister.co.uk/2011/09/17/intel_parallel_javascript/

Comments

0

Using GPU.js, you can compile and run JavaScript functions in parallel on the GPU:

<script src="dist/gpu-browser.min.js"></script>
<script>
    // GPU is a constructor and namespace for browser
    const gpu = new GPU();
    const multiplyMatrix = gpu.createKernel(function(a, b) {
        let sum = 0;
        for (let i = 0; i < 512; i++) {
            sum += a[this.thread.y][i] * b[i][this.thread.x];
        }
        return sum;
    }).setOutput([512, 512]);

    const c = multiplyMatrix(a, b);
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.