0

I recently noticed that my code runs significantly slower since I'm using things like .forEach instead of normal for loops etc.

I know there are a lot of tools that can for instance minify the source code to reduce file size and tools that simply transpile a newer ECMA Script version to an older one.

But I don't think that's what I'm looking for.

I don't really care too much about the file size. Better run-time performance is much more important to me.

Is there a tool like babel that supports this kind of code optimization? For instance optimizing:

values.forEach(add);

to

length = values.length;
for (i = 0; i < length; i++) {
    add.call(context, values[i], i, values);
}
FcoRodr
  • 1,583
  • 7
  • 15
Forivin
  • 14,780
  • 27
  • 106
  • 199
  • [Google Closure](https://developers.google.com/closure/compiler/). – Derek 朕會功夫 Nov 28 '17 at 08:18
  • 1
    if you are already using babel then this can help - https://github.com/vihanb/babel-plugin-loop-optimizer – Ankit Bahuguna Nov 28 '17 at 08:21
  • 2
    Are we talking about real code or some benchmark? These two have almost nothing in common. `since I'm using things like ...` did you check what the actual source of this declined performance is? You only describe a period of time; you may have made other changes that affect your problem. And last, do you actually have a problem, or is this just some premature optimization thing? – Thomas Nov 28 '17 at 08:25
  • FYI your 4th and 10th benchmarks are identical. – JLRishe Nov 28 '17 at 08:37
  • 2
    And about your benchmark, how about using the appropriate tool? That ain't `.forEach()` here. `var sum = values.reduce(add, 0);` with `function add(a,b){ return a + b }` – Thomas Nov 28 '17 at 08:50
  • Thank you guys. The babel plugin semms to do what I wanted. I'm going with Google Closure though, because its more powerful. The code was just a simplified example. `reduce` was also much slower than the raw for loop btw. – Forivin Dec 07 '17 at 08:20

0 Answers0