-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unexpected performance penalty when using field initialiser #33698
Comments
Can you post a reproduction that uses only pure JavaScript? |
@pbadenski thanks, i would recommend reporting this to https://bugs.chromium.org/p/v8/issues/list |
I'm pretty sure this is optimized into a noop. Try to do something like this let instance;
suite.add('A_field_initialized_in_constructor', function() {
instance = new A_field_initialized_in_constructor();
return instance;
});
suite.add('B_field_initialized_as_class_property', function() {
instance = new B_field_initialized_as_class_property();
return instance;
}); and see if it changes the results. |
@lpinca I followed the suggestion - the difference reduced to one order of magnitude, but still surprisingly large:
Test: const { Options, Suite } = require("benchmark");
const runBenchmarkSuite = (suite) => {
return new Promise((resolve, reject) => {
suite
.on("start", (e) => console.log(`Running test: ${e.currentTarget.name}`))
.on("cycle", (e) => console.log(String(e.target)))
.on("complete", (e) => console.log(`Fastest is ${e.currentTarget.filter("fastest").map("name")}`))
.on("complete", resolve)
.on("error", reject)
.run();
});
};
class A_field_initialized_in_constructor {
constructor() {
this.foo = true;
}
}
class B_field_initialized_as_class_property {
foo = true;
}
let instance;
runBenchmarkSuite(new Suite("fields")
.add("A_field_initialized_in_constructor", () => {
instance = new A_field_initialized_in_constructor();
return instance;
})
.add("B_field_initialized_as_class_property", () => {
instance = new B_field_initialized_as_class_property();
return instance;
})
); I'll report to v8. |
Closing as this should be fixed in V8. Feel free to post a link to the V8 issue if it exists. |
I'm getting 2 orders of magnitude performance penalty when using field initialiser. See snippet below (corrected after @devsnek comment):
and the results (using https://www.npmjs.com/package/benchmark):
Environment:
The text was updated successfully, but these errors were encountered: