C#C
C#8mo ago
Core

✅ Class instantiation takes too long

Hello,
A class has many static properties, which are initialized in a static constructor so they’re set up when the first instance is created, and not when individual properties are accessed.
The issue is that there's a method called TryParse which takes way too long, despite having all static properties initialized. The first call to it takes about 3-4 seconds.
I put the TryParse method in the constructor, and here's the result:

c#
var a = new UaDetector.UaDetector();

sw.Stop();
Console.WriteLine($"{sw.ElapsedMilliseconds}ms");

sw.Restart();

var b = new UaDetector.UaDetector();

sw.Stop();
Console.WriteLine($"{sw.ElapsedMilliseconds}ms");

1. instance: 5503ms
2. instance: 1ms


I’m considering a hacky solution where I keep an internal counter and call TryParse only when the object is created for the first time.
I have no idea what could cause this. Any feedback is appreciated.

Below are all the classes involved in the object creation:
  1. https://github.com/UaDetector/UaDetector/blob/main/src/UaDetector/UaDetector.cs
  2. https://github.com/UaDetector/UaDetector/blob/main/src/UaDetector/Parsers/BotParser.cs
  3. https://github.com/UaDetector/UaDetector/blob/main/src/UaDetector/Parsers/OsParser.cs
  4. https://github.com/UaDetector/UaDetector/blob/main/src/UaDetector/Parsers/BrowserParser.cs
  5. https://github.com/UaDetector/UaDetector/blob/main/src/UaDetector/Parsers/ClientParser.cs
Was this page helpful?