-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
Math within vm context is extremely slow #49283
Comments
Thanks for the report but the slowdown isn't unexpected, it's an inevitable consequence of how the vm module is implemented. Executive summary: V8 can normally inline and optimize Math method calls but the vm module intercepts all loads and stores from/to the global object, depriving V8 of the possibility of recognizing such built-in methods. It's why I wrote the vime module years ago, it doesn't suffer from that defect. Closing as a wontfix/cantfix. |
@bnoordhuis, Thanks for the explanation, but I'm not agree with "wontfix".
|
there is a workaround by localizing Math function math_benchmark(name, builtinPow = false) {
const startTime = Date.now();
const Math = global.Math;
for(let i = 1; i <= 100_000_000; i++) {
let x = builtinPow ? i ** 2 : Math.pow(i, 2);
if(i % 100_000_000 == 0) {
x && console.log(name, (Date.now() - startTime) / 1000);
}
}
} but honestly it's looks ugly |
related #30709 |
That's how V8's API works, it's all or nothing. If you feel the documentation is lacking, go ahead and send a pull request. |
Version
14.x,16.x,18.x,20.x
Platform
linux/macos
Subsystem
No response
What steps will reproduce the bug?
Accessing to
Math
withinvm
context is about x50-x100 slower vs native execution:How often does it reproduce? Is there a required condition?
always, tested on all node versions from 14 to 20
What is the expected behavior? Why is that the expected behavior?
it's expected to have nearly same performance
What do you see instead?
it's slower x50-x100 times
Additional information
No response
The text was updated successfully, but these errors were encountered: