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

t.plan is slow #1

Closed
jamestalmage opened this issue Jan 29, 2016 · 5 comments
Closed

t.plan is slow #1

jamestalmage opened this issue Jan 29, 2016 · 5 comments

Comments

@jamestalmage
Copy link

First, thanks for doing this. The AVA team appreciates it.

t.plan is pretty slow. It actually generates an Error and captures the stack trace every time you call it.

It's totally unnecessary for most sync tests, the only times you absolutely should use it are when there is branching or async behavior that might skip an assertion:

  • Promise based tests with a .catch followed by .then

    test(t => {
      t.plan(2);
      return shouldRejectWithFoo().catch(err => {
        // The plan works here to ensure this rejection happens
        t.is(e.message, 'foo');
        return shouldResolveWithBar();
      }).then(result => t.is(result, 'bar'));
    });
  • Guaranteeing multiple callbacks are called

    test.cb(t => {
      t.plan(2);
      const a = () => {
        // the use of `cb` mode, `t.plan` and `t.end` ensures b is called first (and only once), followed by a
        t.pass();
        t.end();
      };
      const b = () => t.pass();
      bThenA(a, b);
    });
  • Ensuring a catch statement happens

    test(t => {
      t.plan(1);
      try {
         shouldThrow();
      } catch (err) {
        // normally t.throws() would be better, but we are testing a non-standard property here.
        t.is(err.foo, 'bar');
      }
    });

I am not arguing with your findings here. I am sure they are accurate. I am also sure your use of t.plan is not the only (or even primary) reason AVA is slower.

@sindresorhus - Maybe this should end up as a recipe as well.

@jamestalmage
Copy link
Author

Just for reference:

import test from 'ava';

for(var i = 0; i < 10000; i++) {
  test(`test${i}`, t => {
    if (process.env.PLAN) {
      t.plan(1);
    }
    t.pass();
  });
}

Results:

PLAN=1 node cli.js --verbose lots-of-plans.js  6.48s user 0.65s system 119% cpu 5.942 total
node cli.js --verbose lots-of-plans.js  1.74s user 0.40s system 154% cpu 1.386 total

@sindresorhus
Copy link

Maybe this should end up as a recipe as well.

👍 Can you open an issue/PR on AVA about it?

@jamestalmage
Copy link
Author

avajs/ava#482 mitigated most of the downside:

PLAN=1 ava --verbose lots-of-plans.js  2.04s user 0.26s system 105% cpu 2.183 total
node ava --verbose lots-of-plans.js  1.42s user 0.26s system 110% cpu 1.526 total

@jamestalmage
Copy link
Author

I will open a PR, but I think I'll play down the performance concern. I don't want people to avoid a good tool because they think it's "slow".

@sindresorhus
Copy link

@jamestalmage Yeah, the perf difference is negligible now. I mostly want it because of best practises.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants