Search code examples
javascriptperformancev8private-members

Javascript Private Property Performance on V8


I have created some tests on jsperf.app and the results are not in line with the charts in this V8 blog post: https://v8.dev/blog/faster-class-features

According to the "interpreted" chart in that link, on Chromium-based browsers, public class fields now initialize at the same speed as a "named property" and private class fields initialize less than 20% slower. Based on this test, on Chrome 115, the latest release, on Windows 10/64, I am seeing both private and public class fields initializing 45% slower (+/-3%) than a property defined and initialized in a class constructor. On Android with the same version of Chrome it's the same, but with a wider +/- range. The private field is always a few percentage points slower than the public one.

This version of the same test initializes 10 of each type of property, and private/public fields are close to 100% slower, so it has a cumulative effect, as you might expect.

What's the story here? I thought that V8 v10.0 was released into Chrome over a year ago.


Solution

  • What's the story here?

    That V8 blog post isn't talking about the speed of eval("class C { pub; }") (which is essentially what your benchmark is measuring); instead it's talking about the performance of new C();. The assumption is that class definitions are rare (you don't really have millions of class declarations, do you?), whereas class instantiations are frequent and performance-critical (your app may well create millions of objects over the course of a run).

    As you can see in this updated version of your test, field initialization performance is indeed pretty much identical in all three cases.

    I thought that V8 v10.0 was released into Chrome over a year ago.

    Correct, V8 10.0 shipped in Chrome 100.