JavaScript: parallel programming via River Trail coming to Firefox

[2012-10-15] dev, javascript
(Ad, please don’t block)

2013-12-23: A new blog post on ParallelJS (as River Trail is now called) supersedes this post.

River Trail is a set of mechanisms that enable a new functional style of parallel programming in JavaScript. An initial prototype was developed by Intel. Recently, work has started to bring it to Firefox.

Intel’s River Trail prototype

[The following description is an excerpt from my free ebook “The Past, Present, and Future of JavaScript”.]

River Trail is an experiment by Intel Labs that adds data parallelism to JavaScript, but without having to explicitly control it, as with WebCL. It introduces the new type ParallelArray with transformation methods that are parameterized via a function implementing the transformation (a so-called elemental function). Arrays have similar methods (e.g., but ParallelArray's methods execute their elemental functions several times in parallel. The following code uses ParallelArray:

    var a = new ParallelArray(1, 2, 3);

    var squares = (x) { return x * x });
    console.log(String(squares));  // [1, 4, 9]

    var cubes = a.combine(function (index) { 
        return a.get(index) * squares.get(index);
    console.log(cubes);  // [1, 8, 27]

The current River Trail prototype is an extension for Firefox. To distribute the work, it uses OpenCL, which must be installed on the operating system. Conveniently, one has the option of using a sequential implementation of ParallelArray, in pure JavaScript, if River Trail is not installed.

More details

[This section is an excerpt from the blog post “Rivertrail” by Niko Matsakis for Baby Steps.]

Parallel arrays have some key differences from JavaScript arrays:

  • They are immutable
  • They never have holes
  • They can be multidimensional but always in a regular way (e.g., in a two-dimensional matrix, each row has the same number of columns)
Parallel arrays support a wide variety of higher-order operations, such as map() and reduce() but also others. See the River Trail specification for the full list. These methods take a function as argument and operate basically the same as those you would find on a normal JavaScript Array. However, there are two key differences:
  • First, the function that is taken as argument is required to be a pure function [which can only change data that it has created (stored in local variables) – including objects].
  • Second, whenever possible, the JavaScript engine will execute the function in parallel.

Modes of execution

There are several conceivable ways of executing River Trail code:
  • Sequentially: a shimmable [1] fallback.
  • Multicore: one worker thread per core.
  • Vectorized: is similar to multicore, but each worker thread will use SSE instructions, allowing it to process more than one array element at a time.
  • GPU: run vectorized code, but on the GPU instead of the CPU.

River Trail in Firefox

When will River Trail be available in Firefox? Sequential execution already works in the development version. A “reasonably functional” version of “multicore” will be added “within a month or two”. After that is finished, “vectorized” will be tackled. “GPU” is farther off.


  1. What is the difference between a shim and a polyfill?