Skip to content
Optimizes statements such as `forEach` and `map` to for loops.
JavaScript TypeScript
Branch: master
Clone or download
vihanb Merge pull request #12 from rajatsharma/fixed-build
Changed preset from es2015 to @babel/preset-env
Latest commit 2e383fb Jan 8, 2020
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Added POSSIBLE_UNDEFINED flag Jan 18, 2019
.babelrc Changed preset to es2015 to @babel/preset-env Jan 8, 2020
.gitignore Update .gitignore Nov 29, 2016
.npmignore Create .npmignore Nov 29, 2016
LICENSE Initial commit Nov 29, 2016 Updated README due to latest changes Jan 9, 2019
package.json version bump since not backwards compatible Jan 20, 2019
test.opt.js It... worked! Jan 18, 2019
yarn.lock Changed preset to es2015 to @babel/preset-env Jan 8, 2020


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


$ npm install babel-plugin-loop-optimizer


Via .babelrc (Recommended)


  "plugins": ["babel-plugin-loop-optimizer"]


$ babel --plugins loop-optimizer script.js

Via Node API

require('babel').transform('code', {
  plugins: ['loop-optimizer']


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:

var m = new Map();
// loop-optimizer: KEEP


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:

let ar = [1, 2, 3]
// loop-optimizer: FORWARD


function timesTwo(arr) {
	return => n * 2);


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

You can’t perform that action at this time.