History

JavaScript got it’s concurrency with web worker but it was like a rocket without fuel since real parallel JavaScript was impossible. Instead data is cloned which is then send to each worker and finally clone the result to reach the main thread. There was no way to do multiple executions on the same data at the same time due to lack of shared memory in JavaScript.

Shared Memory

The above limitation of web worker is going to disappear with the introduction of SharedArrayBuffer. SharedArrayBuffer is a new feature which is recently advanced to ECMAScript Stage 3, means that this feature is a candidate and still has a chance of being included in the next release. Once it is finalized it will be the game changer feature for web developers. When this article was writing, SharedArrayBuffer is available in Chrome Canary, Firefox Nightly and Safari Developer Preview.

Why?

Because this feature will give us superpower to utilize all available CPU cores to distribute computation on the same data at the same time without copying the data back and forth between workers.

WebAssembly

This feature will help asm.js & WebAssembly to achieve full parallel programming but WASM need to wait for post-MVP release. There is no plan to include shared memory in MVP release and it is out of scope for now. Normal JavaScript will get full access to SharedArrayBuffer once it reach Stage 4 and released. But, it is still a primitive form of parallel programming and does not help normal JavaScript developers that much because it is very hard to work with flat memory, that is not so productive. Crafting a complex application using SharedArrayBuffer alone is very unpleasant and time consuming. So we JavaScript developers need a JavaScript like API. For the same reason TurboScript was born.

TurboScript

Oh yeah again a new script but it’s syntax is almost equal to TypeScript. You just need to remember few metadata to decorate your turbo classes and it can compile to normal JavaScript as well as WebAssembly* . Like TypeScript TurboScript need to compile to JavaScript or WebAssembly using TurboScript compiler. The compiler is written in TypeScript. So in a nutshell TurboScript is a scripting language and it’s compiler with a tiny runtime. TurboScript is being developed to simplify parallel programming in JavaScript. Unlike TypeScript, TurboScript requires explicit type declarations. In TurboScript you can create turbo objects which lives in SharedArrayBuffer and can access using pointers from main and all workers hence avoiding copying data back and forth. Since it is not pure JavaScript, you need to compile turbo script to normal JavaScript which will create optimized asm.js like code. Next version of compiler is aiming to output valid asm.js code. For now TurboScript only aiming to bring parallel capabilities to normal JavaScript, therefore you need to inter-operate with TurboScript and JavaScript. Optimal way is to create only long living data objects using TurboScript because TurboScript v1 cannot free allocated objects in a good way in other words; it may cause memory leaks.

Syntax

TurboScript Source Code

Compiled JavaScript Code

What’s Next 

TurboScript can be used for high performance parallel computation like big data processing, image processing etc… For a demo; here is a ray tracing (path tracing) global illumination renderer for three.js

Video demo of three.js ray tracing (path tracing) renderer

 

Performance gain

chart2

To build KD-Tree of Stanford dragon (1,01,926 Triangles), even with shared memory pure JavaScript version took more than a minutes to initialize 8 workers because on the each worker scene need to be copied and build the KD-Tree for fast ray tracing. With TurboScript all workers are holding shared memory pointers to access scene objects and KD-Tree and it only took 8 seconds to build in Chrome and 2.8 seconds in Firefox. There are plenty of room to improve TurboScript performance still it’s a win.

Source Code

You can find the source code of TurboScript in github project TurboScript

Warning! : TurboScript is in alpha stage please experiment but don’t use it for production

I am writing a full featured state of the art global illumination renderer for three.js and my next blog post will cover crafting  complex applications with TurboScript.

Stay tuned!

 

Thanks for reading & Happy New Year 🙂

 

*WebAssembly compiler is broken due to spec changes.
*WebHPC = Web High-performance computing

Screenshot of three.js editor with stanford dragon

Crystal Dragon