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

Performance issue #150

Open
kostia1st opened this issue Apr 7, 2023 · 3 comments
Open

Performance issue #150

kostia1st opened this issue Apr 7, 2023 · 3 comments

Comments

@kostia1st
Copy link

Here's a sample code:

  const deviceDetector = new DeviceDetector();
  const startPoint = performance.now();
  deviceDetector.parse('');
  console.log(`Time elapsed #1: ${Math.trunc(performance.now() - startPoint)}ms`);
  deviceDetector.parse('');
  console.log(`Time elapsed #2: ${Math.trunc(performance.now() - startPoint)}ms`);
  deviceDetector.parse('');
  console.log(`Time elapsed #3: ${Math.trunc(performance.now() - startPoint)}ms`);

Here's the result:

Time elapsed #1: 287ms
Time elapsed #2: 511ms
Time elapsed #3: 514ms

So, I see here a few issues:

  • second run takes almost as much time as the first one, but starting with third everything starts to work faster; given there's some kind of lazy cache, I'd expect the second run already be fast;
  • overall question - is it OK that a fully locally loaded library takes ~280ms of CPU time to process a simple input string? For the context, I run it on AMD Ryzen 7 5800HS - which is a relatively capable CPU
@kostia1st
Copy link
Author

kostia1st commented Apr 7, 2023

After looking into profiler, it looks like the entire time is basically taken by running Regexps (some individual ones run for tens of ms!)

chrome_tBjtOfAQHG
Looks like those MobileParser regexps rock big time.

I'm not into the lib's code yet, but my take is that this could have a major performance improvement.

UPD: Seems like it's rather about initializing RegExp objects. If so, there's little could be done about it I guess.
However, it still isn't clear why it takes 2 attempts to parse in order to create a usable internal cache.

@sanchezzzhak
Copy link

you have the wrong measurement.

  const deviceDetector = new DeviceDetector();
  let startPoint = performance.now();
  deviceDetector.parse('');
  console.log(`Time elapsed #1: ${Math.trunc(performance.now() - startPoint)}ms`);
  startPoint = performance.now();
  deviceDetector.parse('');
  console.log(`Time elapsed #2: ${Math.trunc(performance.now() - startPoint)}ms`);
  deviceDetector.parse('');
  startPoint = performance.now();
  console.log(`Time elapsed #3: ${Math.trunc(performance.now() - startPoint)}ms`);

@WonderPanda
Copy link

In a Google Cloud Run container we've observed that it can take up to 1.3 seconds of CPU time just to parse the user agent. This is enough that it spikes the CPU usage high enough to immediately trigger auto-scaling events from a single function call

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

4 participants
@sanchezzzhak @WonderPanda @kostia1st and others