Closed Bug 1693371 Opened 5 years ago Closed 3 years ago

50% slower JS execution than Chrome on this medium.com page

Categories

(Core :: JavaScript Engine, defect, P3)

defect

Tracking

()

RESOLVED DUPLICATE of bug 1827268

People

(Reporter: mstange, Unassigned)

References

(Blocks 1 open bug)

Details

Profiles: Firefox profile, Chrome profile

During the pageload of https://medium.com/@thefatshadow/how-just-lose-weight-sounds-to-your-fat-friend-f8f644e157e8 , there is a microtask checkpoint which takes 1.1s in Chrome and 1.6-2.0s in Firefox, executing JS.

In that profile, 10% of Firefox JS samples are in s or its nested function, compared to <3% in Chrome. Run through a beautifier, it's still pretty ugly, but looks like this:

function s(e, t) {
    var n = Object.prototype.toString.call(e);
    if ("[object Array]" === n) return e.map((function(e) {
        return s(e, t)
    }));
    if ("[object Object]" !== n) throw new Error("Unexpected input.");
    t && e.loc && delete e.loc, e.loc && (delete e.loc.startToken, delete e.loc.endToken);
    var r, o, i, a = Object.keys(e);
    for (r in a) a.hasOwnProperty(r) && (o = e[a[r]], "[object Object]" !== (i = Object.prototype.toString.call(o)) && "[object Array]" !== i || (e[a[r]] = s(o, !0)));
    return e
}

There are a few less-common features in there (delete, for-in) that we might be optimizing poorly.

Two additional V8 optimisations could also make a difference here:

  • Object.keys() results are cached in V8, so calling it repeatedly on objects with the same shape is fast.
  • for-in + hasOwnProperty is handled specially.

On top of that, bug 1502905 could also come into play:

Running the function from comment #1 through https://unminify.io/ and then manually cleaning it up a bit more and only focusing on the loop, gives us:

function s(e, t) {
  // [Elided...]
  var a = Object.keys(e);
  for (var r in a) {
    if (a.hasOwnProperty(r)) {
      var o = e[a[r]];
      var i = Object.prototype.toString.call(o);
      if (i === '[object Object]' || i === '[object Array]') {
        e[a[r]] = s(o, true);
      }
    }
  }
  return e;
}

That Object.keys() combination with for-in looks a bit nonsensical.

When placed into a µ-benchmark, the performance difference between V8 and SpiderMonkey is quite noticeable:

function f() {
  var xs = [
    {a: 1, b: 2, c: 3, d: 4},
    {e: 5, f: 6, g: 7, h: 8},
  ];
  var q = 0;
  var t = performance.now();
  for (var i = 0; i < 1_000_000; ++i) {
    var x = xs[i & 1];
    var keys = Object.keys(x);
    for (var key in keys) {
      if (keys.hasOwnProperty(key)) {
        var value = x[keys[key]];
        q += value;
      }
    }
  }
  return [performance.now() - t, q];
}
for (let i = 0; i < 10; ++i) {
  print(f());
}

With proper, modern JavaScript, we're just as fast as V8:

function f() {
  var xs = [
    {a: 1, b: 2, c: 3, d: 4},
    {e: 5, f: 6, g: 7, h: 8},
  ];
  var q = 0;
  var t = performance.now();
  for (var i = 0; i < 1_000_000; ++i) {
    var x = xs[i & 1];
    for (var value of Object.values(x)) {
      q += value;
    }
  }
  return [performance.now() - t, q];
}
for (let i = 0; i < 10; ++i) {
  print(f());
}

First, do we care? Unfortunately I think we probably should. Jan, what do you think?

Second: This should be profiled before anyone spends more time on it.

  • Caching Object.keys seems easy. The caching would go in TryEnumerableOwnPropertiesNative whose job is already to optimize keys when possible. Fun patch. But I wonder if that's really the issue. What we're doing there looks reasonable—not as fast as a memcpy, but not terrible.

  • I don't know if eliminating the hasOwnProperty call is possible for us.

  • They're using for-in on an array—which produces an array of strings—and then doing GetElem on the array with those string keys. Is that the slow part?

Flags: needinfo?(jdemooij)
Severity: -- → S3
Priority: -- → P3
Flags: needinfo?(jdemooij)

Looking more closely at this code, it's awful:

var keys = Object.keys(x);            // keys is an array of strings
  for (var key in keys) {             // for-in on an array returns "0", "1", "2", ...
      if (keys.hasOwnProperty(key)) { // This filters out ... properties on the Array prototype?
        var value = x[keys[key]];     // This is an incredibly inefficient way of iterating through an array.
        q += value;
      }
    }

The less terrible version is

var keys = Object.keys(x);
for (var key of keys) {   // or  `for (var i = 0; i < keys.length; i++)`
  var value = x[key];     // and `x[keys[i]]`
  q += value;
}

Using either for-in or Object.keys is reasonable. We've done a bunch of work on the former, and we're working on the latter in bug 1827268. Using for-in on the result of Object.keys is silly, and I don't think it's worth our time to optimize.

Taking a fresh profile of the page today, I don't see any code taking up such a large fraction of samples. The hottest function (c) does contain code like this:

for (var x = {}, E = 0, S = Object.keys(d); E < S.length; E++) {
  var O = S[E];
  x[O] = d[O]
}

But that's a normal use of Object.keys that we already intend on improving elsewhere. The really bad code seems to have been fixed at some point in the last three years.

The important parts of this bug aren't unique, and the unique parts aren't important. I'm going to just dupe it to bug 1827268.

Status: NEW → RESOLVED
Closed: 3 years ago
Duplicate of bug: 1827268
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.