The TC39 committee have, for good reason, ceased active development and relegated SIMD.js/SIMD API from Stage 3. This sampler project was created to demonstrate the general concept and might be useful.
Single instruction, multiple data (SIMD) is a type of parallel processing in Flynn's taxonomy. SIMD can be internal (part of the hardware design) and it can be directly accessible through an instruction set architecture (ISA), but it should not be confused with an ISA. SIMD describes computers with multiple processing elements that perform the same operation on multiple data points simultaneously.
In brief, SIMD is typically implemented at the processor (hardware) level to gain more efficiency but that is not to say the concept does not have utility if implemented at the code level. The mechanism is quite straightforward when implemented using the relatively new (ES2020) allSettled
method of the JS Promise object.
The simd
function taken as its only argument the instruction (function) to be performed on the data and returns a new (reusable) function, specialised for SIMD operation.
The SIMD function expects multiple data items that are converted into an array using the rest operator ...
. The instruction is then used as the call-back of the map method over the data array with a Promise being returned for each call.
The Promise.allSettled method is then used on the resultant array to await completion of all the function calls, being processed in a (near) parallel manner.
This is only a simulation of what (to my understand, which might be wrong) SIMD does. It enables a single instruction to be applied over multiple data items (which is like the array map method but with all operations performed) in parallel. This is not a candidate solution or even a possible polyfil. It is an experiment for educational purposes, that might have utility in production (given sufficient testing).
As an educational experiment is demonstrated how functions can be performed in near parallel manner and how the allSettled method is used to gather the result irespective of the success or failure of each operation.
The two stage calling mechanism employs the functional programming concept of partial application that enables the instruction to be converted into a Promise-base SIMD-like operation. The operation can then be called several times without the need to instrument the instruction each time.
The rest operator is used inside the simd function so as much data as is available at the time can be processed at the same time.
Imagine you have a process that is receiving data via a stream. The amount of data in any given time frame is uncertain but at a regular interval you want all the unprocesed data received to that point, to be processed. This SIMD implementation can produce a function that can take an array of all the unprocessed data and (if called using the spread syntax) can execute the function on all that data in parallel.