proposal-async-context
proposal-async-context copied to clipboard
Consider limiting the number of AsyncContext variables allocated
@syg expressed the concern at TC39 that JS developers may allocate too many AsyncContext variables, slowing down their programs and creating unmanageable expectations on JS engines to make it fast somehow. In general, I think the best practice is for each library or framework to create just one AsyncContext variable. But could we actually enforce a limit on the total number of variables created?
In general, in JS, the specification does not indicate hard limits for real resources like the heap size or the callstack size--it's just implicit that the JS engine may throw an exception or terminate if unspecified limits are exceeded. On the other hand, WebAssembly has standardized its implementation-defined limits at https://webassembly.github.io/spec/js-api/#limits . I wonder if, in JS, we could agree on implementation-defined limits for certain aspects of certain new features, like this.
What if we had an overall limit of AsyncContext variables, after which an exception is thrown if JS tries to allocate more? Maybe this could be 1000 or 10000--I really can't imagine valid use case requiring more than that.
But why?
An unrestricted tool allows you to build a restrictive wrapper around it, but a restricted tool only fails its users who have edge cases.
If you only want 1000 -- or even 1 -- variable, you are able to self-impose these restrictions in your own applications.
Yeah, I think if we come up with a good data structure for the AsyncContext.Snapshot representation (as investigated in this design doc), there shouldn't really be a need to limit the number of AsyncContext.Variable instances allocated. But we might want to leave this issue open to be resolved during Stage 3, to track any issues which may come up.
I'd be strongly -1 on trying to place limits on the number of AsyncContext.Variable instances created.