Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vihanb/babel-plugin-loop-optimizer
Optimizes statements such as `forEach` and `map` to for loops.
https://github.com/vihanb/babel-plugin-loop-optimizer
babel-plugin
Last synced: 3 months ago
JSON representation
Optimizes statements such as `forEach` and `map` to for loops.
- Host: GitHub
- URL: https://github.com/vihanb/babel-plugin-loop-optimizer
- Owner: vihanb
- License: mit
- Created: 2016-11-29T03:47:08.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2020-07-19T06:03:08.000Z (over 4 years ago)
- Last Synced: 2024-04-18T13:11:34.012Z (7 months ago)
- Topics: babel-plugin
- Language: JavaScript
- Size: 90.8 KB
- Stars: 74
- Watchers: 8
- Forks: 7
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-babel - loop-optimizer - Transforms `.forEach` and `.map` calls to for loops. 🔧 (Plugins / Optimization)
README
# babel-plugin-loop-optimizer
**WARNING:** This package is **unmaintained**. It probably still works but I am not able to maintain it. If anyone wishes to pick this up leave an issue and I can give access.
Also, that package aren't working with newest Babel (that have @babel namespace) right now.
Optimizes `.forEach`, `.every`, `.find`, `.map`, `.filter` statements to `for` stements
## Installation
```sh
$ npm install babel-plugin-loop-optimizer
```## Usage
### Via `.babelrc` (Recommended)
**.babelrc**
```json
{
"plugins": ["babel-plugin-loop-optimizer"]
}
```### Via CLI
```sh
$ babel --plugins loop-optimizer script.js
```### Via Node API
```js
require('babel').transform('code', {
plugins: ['loop-optimizer']
});
```## Bugs?
Now you may say "wait, wait, wait!" This optimizes on things that aren't just arrays! My `map#forEach` is optimized too! To fix this, add a comment that says `// O: KEEP` right before the line on which you use some optimized function. Examples:
```js
var m = new Map();
// loop-optimizer: KEEP
m.forEach(f)
```or:
```js
var s = new Set();
// loop-optimizer: KEEP
for (var i = 0; i < 5; s.forEach(f)) {
// ...
}
```This is required since it is not possible to determine an object's type at compile-time.
Also, if you don't want reverse order, you can disable this (optimization) behavior by adding comment `// loop-optimizer: FORWARD` right before the line on which you use some optimized function. Example:
```js
let ar = [1, 2, 3]
// loop-optimizer: FORWARD
ar.forEach(console.log)
```## Example
```js
function timesTwo(arr) {
return arr.map(n => n * 2);
}
```
to:
```js
function timesTwo(arr) {
let _a = arr;
let _f = n => n * 2;
let _r = [];for (let _i = _a.length; _i--;)
_r.push(_f(_a[_i], _i, _a));return _r;
}
```---
If you need any other help, don't hesitate to leave an issue