Search code examples
javascriptgoogle-nativeclient

Expected speedup from using Google Native Client (NACL) for I/O in javascript application?


I am developing a client-side javascript application (to run in google chrome) which needs to load a large amount of data from disk, and currently this is creating a huge bottleneck in speed, enough so that it may render the application unviable.

I was wondering if it is worth looking into building an NACL plugin to solve this problem (ie: using nacl_io)?

Should I expect reading from disk using an NACL-based solution to be significantly faster than javascript, and if so, what kind of speed up should I expect (ie: 2x, 5x, etc)?

I don't know enough about javascript's internal file readers, nor the specifics of NACL's pipeline for that matter, to answer these questions myself.


Solution

  • Native Client connects to the same underlying Chrome interface as JavaScript when reading files, so you likely won't see a significant speedup. In addition, if you read the data in your Native Client module, you'll need send it to JavaScript afterward anyway.

    That said, perhaps you a reading from the disk inefficiently. Try reading from the file in larger chunks (experiment with different sizes: 64k, 1m, etc.) if you aren't already.

    There are two reasons you may want to use Native Client anyway:

    1. If you can also process the data in the Native Client module, and the processing is currently CPU-bound or can be parallelized. Native Client supports threads and intraprocess shared memory. JavaScript supports threads (via Workers) but currently only allows copying or transferring ArrayBuffers, not sharing them.

    2. If you are locking up the web page because your JavaScript code is not yielding in the event loop, you can use Native Client to read the file from another thread. You can block a worker thread in Native Client without stalling the entire app.