Parallel Map

… in JavaScript.

A number of events and coincidences have reminded me about functional programming and parallelism lately, especially an example I saw of Clojure’s pmap:

pmap
function
Usage: (pmap f coll)
       (pmap f coll & colls)

Like map, except f is applied in parallel. Semi-lazy in that the parallel computation stays ahead of the consumption, but doesn’t realize the entire result unless required. Only useful for computationally intensive functions where the time of f dominates the coordination overhead.

I’ve heard, for a long time, the argument that functional programming offers a programming style better suited for parallelism. After all, MapReduce is perhaps the ultimate form of famous-brand massive parallelization. In a typical map operation, you don’t have to worry so much about all the nasty little parallelism problems since your code, by convention, is side-effect free.

parallelmap

So, as my contribution to the world of JavaScript hackery, here’s parallel map in JavaScript:

parallelmap(proc, tasks, callback)

where proc is a function that takes one argument, tasks is an array of things, and callback is a function that will be called with the results when processing has completed. The computation will occur in parallel and show dramatic speedups on processing-intensive tasks.

Continue reading Parallel Map