New issue
Advanced search Search tips
Starred by 2 users

Issue metadata

Status: Duplicate
Merged: issue 2229
Owner: ----
Closed: May 2016
HW: ----
NextAction: ----
OS: ----
Priority: 2
Type: Bug

Sign in to add a comment

Issue 5041: Array.forEach much slower than for() loop

Reported by, May 25 2016

Issue description

Version: 229b66a5176e3d9b5bdf067fa53c6d4f3a00ce12
Architecture: x86-64

The compilation of the following code snippets should result in identical performance, but does not:

    balls.forEach(function (ball) {
      if (ball.bounce()) {
        bounces += 1;

    for (var j = 0; j < balls.length; j++) {
      var ball = balls[j];
      if (ball.bounce()) {
        bounces += 1;

What steps will reproduce the problem?
1. out/native/d8 bounce-d8-slow.js
2. out/native/d8 bounce-d8-fast.js

Expected would be identical performance, but the slow version is about 3x slower.

This code is part of a benchmarking suite aimed at comparing languages, which
is developed here:
At the moment, Node.JS/V8 show a huge outlier on this benchmark because of,
what I assume is a performance bug.
3.4 KB View Download
3.3 KB View Download

Comment 1 by, May 25 2016

Mergedinto: 2229
Status: Duplicate (was: Untriaged)
Why would you expect identical performance? the .forEach version does more work, function calls are relatively expensive (at least compared to "bounces += 1").

Comment 2 by, May 25 2016

I would expect identical or close to identical performance here, because I'd expect that this gets all inlined.

To me it looks like the `forEach` and the lambda aren't inlined for some reason. Is this because of some heuristic not triggering, or is there something more fundamental going on here?

From the language semantics point of view, I wouldn't see any obvious reasons that should prevent it. Or am I missing something?


Comment 3 by, May 25 2016

Well yes, a sufficiently smart compiler could inline it. Currently we don't. It's not as trivial as it may seem at first glance though. (Who's to say that the .bounce method doesn't fetch bounce.caller, the closure you expect to be eliminated.) The performance of .forEach will always be at least a little worse (requires more expensive warm-up), and will need more expensive tracking that a simple for-loop.

Comment 4 by, May 30 2016

Project Member
The following revision refers to this bug:

commit eff24bef5c0f071008bdd4bcee3a86384e90c90b
Author: cbruni <>
Date: Mon May 30 10:01:14 2016

[array] speed up array.forEach and friends by directly using in-operator

The in-operator has recently been optimized making our smart macros obsolete.
This should give up to 10% speedup on forEach and friends.

BUG= v8:5041 

Cr-Commit-Position: refs/heads/master@{#36576}


Comment 5 by, Mar 23 2017

Labels: Priority-2

Sign in to add a comment