Greenlet – Move an async function into its own thread in the browser
github.com
github.com
greenlet is a bit like "workerize-lite" in the sense that it allows you to move individual functions to their own thread.
Also relevant: a discussion with Jason yesterday about when it's worth moving functions to a web worker: https://twitter.com/mxstbr/status/957312201987063808
Does anyone know of write-ups documenting performance of the various methods of pushing data to and from WW across different browsers?
https://github.com/developit/greenlet/blob/master/greenlet.j...
(The issues that were discussed in this thread had been resolved by the time I wrote this comment, and so the link above pointed to the wrong commit.)
the greenlet function when invoked returns a promise waiting to be resolved or rejected. the resolver and rejector functions are stored in object p with the key with a unique call id.
so line 18 `p[c][e?1:0](e||d);` basically means resolve or reject the promise based on the parameters from the web worker message by invoking either the resolve or reject function stored by its call id.
here's my quick attempt to understand and annotate this lib.
https://gist.github.com/zz85/25564f1910f1877c39c25ace5e5159b...
I haven't touched Javascript for a long time, especially using shiny features in ES5, ES6... those arrows always throws me off. Is this a typical JS & NodeJS style nowadays? Does anyone ever feel your code and your co-workers' code are barely readable?
Disclaimer: Python programmer.
* def vs lambda in Python
* fn vs closures in Rust
* def vs anonymous functions in Elixir
Honestly, given how often I find myself using small, immediately-consumed functions when performing common filter/map/reduce operations I find fat arrows to be an extremely welcome addition. I feel that it spares a lot of excessive "function" keywords everywhere and gives you just a little more room to have more verbose variable names when you have to deal with things like enforced maximum line lengths...
This is really a util method to simplify the call syntax for a web worker.
That's a pretty major caveat and makes the project's tagline basically false. It's just a hacky way to spawn a Web Worker on the fly by evaluating a string that was made by coercing a local function declaration into a string.
* read request
* parse from JSON to an object
* read the data from the object in the worker to the main thread
* create a new object in the main thread with the data from the worker
Those last 2 steps are about as slow as a JSON.stringify and JSON.parse, and are completely unnecessary. As others have said, adding some filtering to the example makes this example worlds better.
Greenlet would execute the function in an entirely different thread, which isn't possible without the Web Worker API.
Someone please correct me if that's wrong!
That means that any I/O doens't block the main thread, and other parts of JS can execute.
This example provides literally no benefits, and actually is a performance hit from converting the data from JSON, then sending it back to the main thread (which itself copies the data in a method similar to converting back to JSON then back to an object again in the main thread).
(I had to do exactly that for a B2B app that was receiving hundreds of MB of data a while back)
> The name is somewhat of a poor choice, but it was available on npm.
npm has supported scoped packages [0] for years now, and it's a fantastic solution to this problem, as well as solving many others (like typosquatting in many cases).
I know this package is already named, but I really urge people to use scoped packages more.
What I would like to see is all new unscoped packages being able to be installed/required by their scoped version by default. So if I create a package `coolThing` in npm entirely unscoped, it would be installable by doing `npm -i @Klathmon/coolThing` and by doing `npm -i coolThing`
That would let many of us to use the benefits of scoped packages without the package authors having to do anything. And it would be a big step toward going completely scoped at some point in the future.
Doesn't seem especially tricky.
Docker and go use URLs/paths for scoping, everything is scoped.
Java/Scala/Kotlin/XText/etc use reversed domains as scope.
And so on.
We can talk about how awful of an idea it was for npm to start the way they did all we want, but the reality is that they are in the position of unscoped packages being the "default" right now, and I'd love for them to get away from them in a safe manner.
- https://github.com/angular/angular
- https://github.com/rollup/rollup
- https://github.com/cherow/cherow
- https://github.com/hyperapp/hyperapp
Wouldn't we end up with a similar situation?
It also allows those orgs to group "sibling" or sub-packages under their main name. So I know that `@angular/cool-angular-plugin` is actually from angular.
But for the downside of "having to type a little bit more", I think it's more than useful enough at what it does fix/solve.
In other words, it solves between 0 and "a bunch" of problems for packages, and the downside is fixed at "needs to type a little more".
Even in the worst case, it's not that much harm, but in the best case could prevent exploits.
If one does the same on npm, it just transfers the namespace scarcity to 'scopes' themselves, with no real benefit.
Btw it's better to email us (hn@ycombinator.com) than to try to get our attention this way, since we don't come close to seeing all the comments, or even all the threads.