question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Performance issues with `userEvent.type()`

See original GitHub issue
  • @testing-library/user-event version: 12.6.0
  • Testing Framework and version: @testing-library/react version 11.2.1
  • DOM Environment: jsdom version 16.4.0

Relevant code or config:

describe("demo", () => {
  const text = "2001:db8:ac10:fd01::20";

  test("time type", () => {
    render(<input data-testid="input" type="text" />);
    const input = screen.getByTestId('input');
    const before = new Date();
    userEvent.type(input, text);
    console.log(`type took ${new Date() - before}ms`);
    expect(input.value).toEqual(text);
  });

  test("time delayed type", async () => {
    render(<input data-testid="input" type="text" />);
    const input = screen.getByTestId('input');
    const before = new Date();
    await userEvent.type(input, text, {
      delay: Number.MIN_VALUE,
    });
    console.log(`delayed type took ${new Date() - before}ms`);
    expect(input.value).toEqual(text);
  });

  test("time paste", () => {
    render(<input data-testid="input" type="text" />);
    const input = screen.getByTestId('input');
    const before = new Date();
    userEvent.paste(input, text);
    console.log(`paste took ${new Date() - before}ms`);
    expect(input.value).toEqual(text);
  });
});

Problem description:

Running the above test suite with yarn test --no-cache logs the following values on my machine:

type took 29ms
delayed type took 63ms
paste took 1ms

The results are fairly consistent between reruns (plus-minus a few ms).

The performance difference is starker with longer strings: replacing the input text with const text = "2001:db8:ac10:fd01::20".repeat(100);, the results are as follows:

type took 1380ms
delayed type took 5940ms
paste took 1ms

I understand that some of this difference stems from the letter-by-letter nature of type() which is crucial for certain test scenarios. For the time being however, type() is too slow for many of the tests we run and we’ve had to opt for paste() instead.

I haven’t profiled type()'s internals so I don’t know what the source of the performance penalty is.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:17 (8 by maintainers)

github_iconTop GitHub Comments

19reactions
Etherytecommented, Mar 8, 2021

I don’t think the scale of the problem is as low as you make it to be. If you do ten type() calls within one test you’re already getting close to a second and with anything larger you can quickly reach Jest’s default timeout of 5 seconds. Choosing to not look into this is fair, but I don’t think downplaying the problem is beneficial.

For context, in a perfect world I would expect type() with no delay to be on the same scale as paste(), or at least in the same order of magnitude. Right now it can be several orders of magnitude slower, easily reaching the default time limit.

16reactions
ph-fritschecommented, Jul 7, 2022

@mnbroatch Thanks for your feedback. Have you tried turning it off and on again?

Your detailed information about the environment will certainly allow us reproduce the problem and fix it for you. We’ll be working day and night on this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

testing-library/user-event 's type() is slow - Karl Tarvas
The type() command is great for simulating user input letter-by-letter, however it has some issues: without a delay, it is non-deterministic ...
Read more >
user-event v13 - Testing Library
Simulates the keyboard events described by text . This is similar to userEvent.type() but without any clicking or changing the selection range.
Read more >
React Testing Library userEvent.type recognizing only the first ...
UPDATE: Here is my component code. Component is very simple with an input box with onKeyDown event. const SearchBox = () => {...
Read more >
NetSuite Applications Suite - User Event Script Execution Types
Cloud · Cloud Applications · NetSuite. NetSuite Applications Suite. Table of Contents; Search. Contents. Expand AllCollapse All.
Read more >
React Testing Library Cheatsheet - Codecademy
The React Testing Library (RTL) provides a render() method for virtually rendering React components in the testing ... userEvent.type(textbox, 'Hello!');.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found