Deep Dive: SIMD JavaScript Code Share your comment!

Posted on by Jeff Cogswell, Slashdot Media Contributing Editor

The future of JavaScript will likely include SIMD capabilities. In the second of this two-part series, Jeff Cogswell looks at some of the test code developed by Intel and Google and explains how it works.

Let’s take a look at some sample SIMD JavaScript code. We can’t actually use this code yet to test out the SIMD features, because we don’t have access to the correct JavaScript runtime engines that support it. But by looking at this code, we can get an idea of what the engineers at Intel and Google are working on to add SIMD support, which will hopefully eventually find its way either into the browsers or, at the very least, an open source branch that we can use.

 You can find the code on GitHub. Note that this code is by no means final, so please don’t rely on it for production purposes. This code is an early proposal for what the API should look like. Inside this repository you can find the JavaScript version in the src directory. Inside there, the main source file is ecmascript_simd.js. When you look at that, you can see that this file starts out by creating a SIMD namespace. (Namespaces don’t actually exist in JavaScript, but an equivalent can be created using objects. The SIMD namespace, therefore, is just an object stored in a variable called SIMD.)

 The SIMD namespace includes a constructor function called float32.4. (For the JavaScript experts reading this, notice that the code can be called as constructor or as a regular function. In the case of the latter, the function calls itself as a constructor.) This is the main constructor for a 32-bit floating point number stored in a 128-bit SIMD register. There is also a float64.2 function for storing 64-bit floating point numbers.

 But farther down in the code we can see something interesting in the SIMD arithmetic functions. Here’s the code for the add. This code apparently adds four 32-bit floating point numbers simultaneously in a single register through a vectorized addition:

 SIMD.float32x4.add = function(a, b) {

            return SIMD.float32x4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);


 What I find fascinating about this code is it looks like it just calls float32x4, passing in the sum of the four numbers with the second set of four numbers. It just adds a.x to b.x, and so on. That means the code would function as regular-old JavaScript code. However, this is why they probably had to change the engine to explicitly recognize that this is intended to be SIMD code and essentially auto-vectorize the code for you. And if you’re running on a regular engine that doesn’t have SIMD capabilities, then you’ll just get the plain old JavaScript addition. See how that works? Pretty cool.


 One particularly interesting aspect about this code is that it was written by a guy who works at Google and is active in Google’s Dart language. Dart was created to be a replacement to JavaScript as a browser language. There’s a lot of resistance within the browser community to Dart, however, so I’m not saying it will definitely take hold. Regardless, this sample code lives in a repository owned by a Google Dart engineer who has also added SIMD capabilities to Dart. This JavaScript code is specifically mentioned by the Intel engineer who also mentioned that Intel is working with Google on this project.

 Will the browser JavaScript engines officially support SIMD? I hope so, and I strongly suspect so as people want to see more and more performance from their JavaScript apps. And let’s not forget the server-side JavaScript (such as node.js) that is also becoming popular. We can hope that that platform also gets SIMD.

Part 1: Adding SIMD Vectorization to JavaScript