Skip to content
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

Cryptographic operations are 150 times slower in Jest compared to node #9006

Closed
naddison36 opened this issue Oct 4, 2019 · 3 comments
Closed

Comments

@naddison36
Copy link

🐛 Bug Report

When I run a JS function that does cryptographic operations on node.js is takes around 400 milliseconds. When I run the exact same JS code in a Jest test, it takes a minute.

To Reproduce

git clone https://github.com/jpmorganchase/anonymous-zether
cd anonymous-zether/packages/anonymous.js/
npm i
cd src

Then create a js file with the following code. eg commitPerfTest.js

const { GeneratorParams, FieldVector } = require("./prover/algebra.js");
const bn128 = require("./utils/bn128.js");
const params = new GeneratorParams();
params.extend(64);
const gExp = new FieldVector(Array.from({ length: 64 }).map(bn128.randomScalar));
const hExp = new FieldVector(Array.from({ length: 64 }).map(bn128.randomScalar));

const startTime = new Date();

params.commit(gExp, hExp, bn128.randomScalar());

const endTime = new Date();
console.log(`Commit took ${endTime - startTime} milliseconds`);

Run the code. On my MacBook Pro this takes around 400ms

node commitPerfTest.js

Now run the above code in a Jest test. eg commitPerf.test.js

test("commit perf test", () => {
    const { GeneratorParams, FieldVector } = require("./prover/algebra.js");
    const bn128 = require("./utils/bn128.js");
    const params = new GeneratorParams();
    params.extend(64);
    const gExp = new FieldVector(Array.from({ length: 64 }).map(bn128.randomScalar));
    const hExp = new FieldVector(Array.from({ length: 64 }).map(bn128.randomScalar));

    const startTime = new Date();

    params.commit(gExp, hExp, bn128.randomScalar());

    const endTime = new Date();
    console.log(`Commit took ${endTime - startTime} milliseconds`);
    expect(endTime - startTime).toBeLessThan(1000);
});

Now run the Jest test assuming you have Jest installed globally

jest commitPerf.test.js

On my laptop, the last params.commit(gExp, hExp, bn128.randomScalar()); line takes around 60000 milliseconds. I've also tested with Jest 23 and 22 and they also take around one minute.

Expected behavior

I'd expect Jest to add some overhead and could live with it being 2, even up to 10 times, slower than node. But 150 times slower is unworkable.

Link to repl or repo (highly encouraged)

https://github.com/jpmorganchase/anonymous-zether
Consensys/anonymous-zether#12 (comment)

envinfo

I was testing using Jest 24.9.0

System:
OS: macOS Mojave 10.14.6
CPU: (8) x64 Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz
Binaries:
Node: 12.11.1 - ~/.nvm/versions/node/v12.11.1/bin/node
Yarn: 1.19.0 - /usr/local/bin/yarn
npm: 6.11.3 - ~/.nvm/versions/node/v12.11.1/bin/npm

@SimenB
Copy link
Member

SimenB commented Oct 4, 2019

This is a duplicate of #5163. Adding

{
  "jest": {
    "extraGlobals": ["Math"]
  }
}

to package.json results in this:
image

@naddison36
Copy link
Author

Wow, that simple config change has the test running in 600 ms now on my laptop. That's a 100x improvement! Thanks @SimenB

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants