Skip to content

getBrowserName performance improvement#89

Closed
qz4 wants to merge 3 commits intoActivityWatch:masterfrom
qz4:master
Closed

getBrowserName performance improvement#89
qz4 wants to merge 3 commits intoActivityWatch:masterfrom
qz4:master

Conversation

@qz4
Copy link
Copy Markdown
Contributor

@qz4 qz4 commented Aug 1, 2022

Performance improvement on the getBrowserName function.

  1. app.js file size goes from 115.8KB -> 73.5KB
  2. Using String.prototype.includes() instead of Regex
  3. Avoid .exec() on many regexes (From ua-parser-js dependency)

Negative impact being that aw-watcher-web will not detect more rarely used browsers. This might be justified due to the application only being on the Firefox and Chrome addon store and the ease of adding support for a missing browser. The browser name "unknown" is used in the case of an unsupported browser.

I tested this on Firefox only.

Copy link
Copy Markdown
Member

@ErikBjare ErikBjare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good to me!

I'm not entirely convinced it will treat browsers the same as before, however, so some tests would be even better :)

Comment thread src/client.js
Comment on lines +44 to +60
getBrowserName: function () {
if (
navigator.userAgent.includes("Opera")||
navigator.userAgent.includes("OPR")
) {
return "opera";
} else if (navigator.userAgent.includes("Firefox")) {
return "firefox";
} else if (navigator.userAgent.includes("Brave")) {
return "brave";
} else if (navigator.userAgent.includes("Chrome")) {
return "chrome";
} else if (navigator.userAgent.includes("Safari")) {
return "safari";
} else {
return "unknown";
}
Copy link
Copy Markdown
Member

@ErikBjare ErikBjare Jun 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these in the correct order? (looks correct, but not sure)

Some example User-Agent strings (by GPT-4):

Google Chrome:     Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36
Mozilla Firefox:   Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0
Apple Safari:      Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Safari/605.1.15
Microsoft Edge:    Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.48
Opera:             Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36 OPR/64.0.3417.83
Internet Explorer: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; AS; rv:11.0) like Gecko
Brave:             Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36 Brave/1.21.73

I would have a lot more confidence in this big if-statement if we had some tests for the above examples.

Also:

  • Add Microsoft Edge

Copy link
Copy Markdown
Member

@ErikBjare ErikBjare Jun 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's also a list of browsers in aw-webui's queries.ts: https://github.com/ActivityWatch/aw-webui/blob/1f194ff6886d82ac5928e8cb944888d378c54157/src/queries.ts#L216

We should check so that the entirety of that list is covered. Right now I see it's missing Edge, Orion, and Vivaldi.

Might want to add a comment referring to queries.ts so that it can easily be cross-checked when modifying it in the future.

@qz4
Copy link
Copy Markdown
Contributor Author

qz4 commented Jun 25, 2023

I updated the code to support Edge.
I've tested that the new changes work in Firefox.
Due to having no existing tests set up for this repo as well as being short on time, I've for now only performed tests locally on a few user agents, with the UAs you mentioned included.

chrome - Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36
firefox - Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0
safari - Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Safari/605.1.15
edge - Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.48
opera - Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36 OPR/64.0.3417.83
unknown - Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; AS; rv:11.0) like Gecko
brave - Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36 Brave/1.21.73
edge - Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.43
chrome - Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36
unknown - Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko
edge - Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299
firefox - Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/110.0
chrome - Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36

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

Successfully merging this pull request may close these issues.

2 participants