A little tool who can be usefull !
Talking to my friend Pascal about webworker and gpujs, we had like a brainstorming.
“It will be nice if we can transform a code without sideeffect on gpu and/or webworker!”
I thinked a bit about it, funny idea…
After it I looked about how is made GPUJS, fastly… It use a library for make an AST of your code, and transform it in a shader.
Very good, but shader have lot of limitation:
- in input, you can have just constant, and until 3 arrays of 1 or 2 dimensions.
- avoid “if” instruction as hell, and when you write in JS, it is not the natural way to think.
But that’s still possible: give to GPUJS a code, and if it fail to compile, was not good!
but it need prepare the input parameters…Bit difficult.
But for webworker ? and it is usefull ?
It is very easy in fact !
I did it, it is not perfect, but that’s work :
1 | function makeWorker(workerFunction, cb) { |
What we can improve, and when that’s can be usefull ?
First, in one case,we have a problem : if the function use global variable.
We can detect it by try call comand in a try catch, and error will appear in such case.
More difficult: if it use gloabl variable BUT don’t change them… need an AST tool in such case.
We can also make it a “promise” in easy way.
Another bonus: we can make it webworkker AND memoize ( just a strange word for say: “if this function is pure, same arguments, will give same result, let’s do a cache !!!”)
but more !
As I say we can make them look like a promise…with a big benefits on real promise !
Let’s look to the API Promise in javascript :
Most of the time, just await and async are enough, but not in all case as :
mdn doc on Promise.any
In such case, we continu execution if ONE function is finish….but the other continue !
Let’s imagine a sitation as:
- try get a data on indexedDB (fast,but not sure)
- or get by AJAX to cdn1
- same cdn2
- take it a complicate and slow way
With promise.any …. yes that’s work, you have your data as soon as possible but the other stuff continue !!!
As webworker have a “terminate()” function, we don’t have this problem.
Thank you for reading ! I will try post on such subject soon !