All threads / [AskJS] Are there Babel plugins that optimize code for special use cases?
Ask A Question


You’re not receiving notifications from this thread.

[AskJS] Are there Babel plugins that optimize code for special use cases?

TrevorGage asked in General

Hey all! I was thinking a bit - I don't really use generators, and I suspect that might be because you can't just use map, filter etc. Of course you can use the [...gen] way, but that gets very memory heavy with a non-small amount of items.

But we could actually optimize this. It shouldn't be too difficult to write a Babel plugin that optimizes chains of array method calls on a spread generator by essentially replacing the chain with a transducer that exactly matches behavior (if that is possible). I mean, we could theoretically even optimize these chains on all arrays (although determining what an is an array is probably not possible beforehand). Since this doesn't touch any prototypes and doesn't change behavior it shouldn't cause any problems, and the syntax is still exactly the same as before and understandable to everybody.

Does anything like this exist (doesn't have to be this case, any "optimization" plugin would be interesting)? If not, would you be interested in trying a plugin like this?

Ideally something like this could even be proposed as a language addition if it turns out useful based on developer feedback and real-life benchmarks.

I really, really don't like the idea of modifying the iterator prototype for these cases. And honestly I don't think they are really necessary. Yes, map, filter etc. should return the type they are acting on, but since that is explicitly an array with the spread, this is not a problem.

There is also no need to differentiate between a spread array and a spread generator, both could be auto-optimized not to create intermediary arrays and instead act similar to a transducer.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 64,380+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2022 GoRails, LLC. All rights reserved.