Amplitude-JavaScript icon indicating copy to clipboard operation
Amplitude-JavaScript copied to clipboard

Expensive init due to inefficient UserAgent parsing

Open AaronO opened this issue 6 years ago • 0 comments

Cross posting this from https://github.com/faisalman/ua-parser-js/issues/401 (I know this SDK uses your own fork, but issues are disabled on your fork):

A big chunk of Amplitude's initialization time is spent in ua-parser's getResult(), see profile below:

image

This profile is for Chrome running on macOS on a MacBook Pro.

Given that our detection logic is basically, scan through the userAgent string with a list of regexes until one matches then stop. The worst case is O(n) where n is the number of regexes (per feature we're detecting, browser/os/device/etc ...)

We can't optimize the worst case scenario without radically changing this library's design (e.g: to a one-pass parse of the userAgent), but we can significantly improve the performance of the common case for popular configurations.

If we focus on the list of os regexes, macOS & iOS are near the end, whereas less popular OSes (e.g: blackberry, nintendo/playstation, ...), which means the rgx() call with have to test all those lesser popular platforms before getting to iOS/macOS.

The same is true for the browser regexes, Chrome is near the end of that list despite being the most popular browser on the market.

Additionally there doesn't seem to be an overlap between regexes that would require running some of them before others for correctness. So it seems like a no-brainer to sort regexes by their popularity (Chrome, Edge, Firefox, Safari, ...), (Windows, Android, iOS, Mac, ...)

Further improvements

Additionally it seems like this SDK only uses the outputs of getBrowser() and getOS(), so we could remove the getDevice() call/code, to make it even faster.

AaronO avatar Nov 26 '19 14:11 AaronO