Badass JavaScript

A showcase of awesome JavaScript that pushes the boundaries of what's possible on the web, by @devongovett.

The JavaScript DSP API, RiverTrail, and the Future of JavaScript Parallelization

January 4th 2013

A few months ago, Opera drafted a spec for a new DSP (Digital Signal Processing) API for JavaScript.  This API would enable access to special vector instructions available to modern processors from JavaScript, and could possibly enable speedups of vectorizable JavaScript code.  Instead of writing a for-loop to do the same computation to every element in an array in serial, you could call a method from the DSP API to perform some common mathematical operations to an entire array in parallel.

The DSP API spec also includes an FFT (Fast Fourier Transform) interface, and a Filter interface supporting both FIR (finite impulse response) and IIR (infinite impulse response) filters, commonly used in signal processing applications.  This could enable additional speedups over existing JavaScript implementations of such features.

Unfortunately, no browser vendor (including Opera I believe) has actually implemented the DSP API.  However Jussi Kalliokoski, who is part of the Web Audio working group at the W3C and a co-author of the WebMIDI spec, has been working on a Node.js implementation of the API called node-dsp.  It is of course implemented as a C++ module, but enables the API’s use in JavaScript.

The DSP API is not the only attempt at adding support for some sort of parallelization to JavaScript.  Firefox and Intel have been working on the RiverTrail project, which adds a ParallelArray type to JavaScript, which can be operated on using methods like map, reduce and forEach like you’d find on normal JavaScript arrays.  The difference is that the callback functions you pass to those ParallelArray methods are executed in parallel as long as they have no side effects.

The original RiverTrail project compiled JavaScript functions to OpenCL kernels which could then be executed on the GPU (or CPU I think).  Firefox Nightly has support for a ParallelArray implementation as well, so you can try it out there today.  Firefox is planning to support a number of possible parallelization schemes, including using worker threads (multicore), using special vector SSE instructions, and running on the GPU.

This is exciting work, but it is much more general purpose than the DSP API, which provides a few prewritten functions developers can use whereas RiverTrail/ParallelJS is trying to parallelize custom JavaScript functions.  You can find out more about RiverTrail and the work going on in Firefox over on Nicholas D. Matsakis’ blog.

Personally, I’d like to see more browser vendors getting involved with parallelizing JavaScript and implementing the DSP API, ParallelArray, and anything else that could help in this area.  JavaScript engines are getting really fast these days but it is unfortunate that JS programs can only access a single core for the most part, and cannot make use of the speed boosts that vector instructions and GPUs can offer in modern computers.

For now, you can try out the DSP API in Node.js using node-dsp (just dsp on npm), and the ParallelArray support in Firefox Nightly.  Hopefully 2013 will have more to bring in this area!