-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Calls to Math are several orders of magnitude slower than when run outside of Jest #5163
Comments
It seems like it could be some issue of optimization at a lower level perhaps because a single call, or even ten calls, to |
This is really interesting... It went from 2000ms to 1000ms by just injecting I think we somehow hit some deoptimisation, but I'm not sure how to debug that. Thanks for the focused reproduction! |
My suspicion is that this is somehow to do with parallel virtualisation of tests, which uses workers. |
Using |
Does jest affect babel configuration in any way or does it take whatever's in .babelrc? |
cc @fhinkel do you ave any insights as to why Math would behave so much differently inside of a vm context vs outside? |
I don't know of an obvious reason why |
Yeah that makes it even more odd, we are not touching the Math object created using the VM module so I’m unclear what would cause it to be slower than normal. |
@mattblang thanks so much for providing a focused repo and instructions so I didn't have to guess what to run and what to time 👍 . I digged into this a little 🕵️♀️: Unfortunately, it's the normal overhead that comes from using the vm module. The vm module uses interceptors for every global property and the interceptors are not cheap. If the code heavily uses global variables, the slowdown is larger. It's not specific to Jest uses the vm module. If the vm module doesn't find a property, e.g., I'm assuming you opened this issue because that slowdown is a real world problem for I added one line to script_transformer.js:
|
@fhinkel thank you so much for digging into this, this is really really useful. |
Looks like we can close this now :) |
@thymikee Well, we understand the cause now but it is still a problem that renders stuff like jest-image-snapshot almost unusable. Should this issue be left open? |
I agree. Not sure about the best solution, though |
@Jack89ita that should no longer be an issue. I made some changes in Jest-image-snapshot awhile back to fix the perf issues. Can you open an issue in Jest-image-snapshot with a repro? A test taking 20 seconds is way beyond normal. |
@anescobar1991 thank you for your answer. I was doing some test and i noticed that for taking a new snapshot it takes around 7s, but if i already have one the comparison takes about 20s. I am taking a fullpage screenshot with puppeteer at hight resolution (1920px width, the height depens on the page). |
@Jack89ita A few hundred milliseconds at the very most. |
@anescobar1991 wow..that's a big difference. may this be caused by the images dimensions? |
Possibly but hard to tell without a repro. |
Hi @anescobar1991 , as you mentioned:
I would like to know where these changes are; |
@ajhsu this was solved in v2.0.0 that was released awhile back, so taking 2 minutes to compare is not normal. Either way can you open an issue in jest- image-snapshot? Let’s not spam this jest issue. |
for me - not really. I will dig into it at weekends paeth predictor is that https://github.com/lukeapage/pngjs/blob/master/lib/paeth-predictor.js#L3 Yup comment #5163 (comment) tells about it @fhinkel If understand properly, there is no way to resolve this issue - because of lookup of global variable? |
I'm also experiencing this, ballooning what was a <1s test suite in mocha to a 2 minute (!!) test suite 😞 Would it be problematic to automatically do this for commonly used globals based on a config property as suggested? |
Using jest 24 and the option added in #7454, these are the numbers I'm seeing with the repro in the OP:
vs without changes:
Diff: diff --git i/package.json w/package.json
index df1978c..61333f9 100644
--- i/package.json
+++ w/package.json
@@ -6,7 +6,7 @@
"author": "",
"dependencies": {
"jasmine": "^2.8.0",
- "jest": "21.2.1",
+ "jest": "24.0.0",
"pixelmatch": "4.0.2",
"pngjs": "^3.3.1",
"pngjs-nozlib": "1.0.0",
@@ -17,6 +17,9 @@
"test": "jest"
},
"jest": {
+ "extraGlobals": [
+ "Math"
+ ],
"testEnvironment": "node"
}
} |
The tests were initially super slow, as the PNG.sync.write took up to 20 seconds. This seems to be related to jest [1][2]. I therefore added Math to the sandboxInjectedGlobals to speed things up. [1] pngjs/pngjs#96 [2] jestjs/jest#5163 (comment)
Any particular reason this was closed? PNGjs lib maintainers believe this is still an issue. At least on jest 29.5 with some of pngjs I was seeing performance hits around 50x. I spent half an hour trying to setup worker threads before realizing that jest was causing massive slowdown in at least one particular function: PNG.sync.write |
Yeah, the comment before yours. And the issue you linked to has been closed for 5 years This is a limitation of how Node's |
(Locking as this issue has been closed for more than 4 years - not sure why the bot hasn't done so) |
It appears that at least some calls to Math (only tested
abs
andacos
) are several orders of magnitude slower than when run outside of the Jest environment.See this repo and follow the first list of instructions to reproduce the issue. Running
Math.abs
ten million times takes about10ms
outside of Jest, but increases to about2000ms
within a Jest environment.OS: macOS High Sierra 10.13.2
Jest: 21.2.1
Node: 8.9.1
NPM: 5.5.1
The text was updated successfully, but these errors were encountered: