Search code examples
javascriptmultithreadingperformanceweb-worker

javascript web workers multithreaded string search slower than single thread?


I have a fuzzysearch function. I have a list of 52k words. I'm running the function against each word. it takes about 30ms to finish.

i tried splitting it up into 8 web worker threads by sending 1/8th of the list to each worker (i have 8 threads on my cpu) using myWorker.postMessage({targets:slice, search}). but this is much slower, around 100ms.

my question is: is it possible for multithreaded to be faster here? or is it simply too much data to copy around to finish in under 30ms threaded? is it possible to not copy the memory and have some kind of shared memory?

(it seems like just simply sending the data to the workers is slower than me actually searching all the data in 1 thread!)


Solution

  • is it possible to not copy the memory and have some kind of shared memory?

    You can use second parameter of Worker.postMessage() to transfer the created object from Worker thread to main thread, or from main thread to worker.

    // transfer data to `Worker` instance
    worker.postMessage(data.buffer, [data.buffer]) // where `data` is an `ArrayBuffer`
    

    // transfer data from `Worker` instance
    self.postMessage(data.buffer, [data.buffer]) // where `data` is an `ArrayBuffer` 
    

    Passing data by transferring ownership (transferable objects)

    Google Chrome 17+ and Firefox 18+ contain an additional way to pass certain types of objects (transferable objects, that is objects implementing the Transferable interface) to or from a worker with high performance. Transferable objects are transferred from one context to another with a zero-copy operation, which results in a vast performance improvement when sending large data sets.