james-fray / local-cdn

WebExtension implementation of Decentraleyes project: Local emulation of Content Delivery Networks
http://add0n.com/local-cdn.html
Mozilla Public License 2.0
170 stars 41 forks source link

Optimize the code #6

Open kurtextrem opened 7 years ago

kurtextrem commented 7 years ago

There is a lot of other noise in this commit, which you don't want to backport, but also quite a few optimizations: https://github.com/kurtextrem/local-cdn/commit/c1370bfdcc1e3f0431bfba4907e2ab3cad7fbdf9

a) V8 needs to compile the regex each time the callback function of chrome.webRequest.onBeforeRequest.addListener is called. Declare the regexes outside the cb function to improve performance.

b) Cache flattenObject responses. The for..in loop isn't exactly cheap to do. See here: https://github.com/kurtextrem/local-cdn/blob/c1370bfdcc1e3f0431bfba4907e2ab3cad7fbdf9/src/common.js#L32 & https://github.com/kurtextrem/local-cdn/blob/c1370bfdcc1e3f0431bfba4907e2ab3cad7fbdf9/src/common.js#L43

c) Cache getURL. Extension URL won't change as long as the background is active.

d) Make the chrome.webRequest.onBeforeRequest.addListener CB fn an own function - like function callback(data), this makes the extension more testable (test commit here https://github.com/kurtextrem/local-cdn/commit/63d60bfe6aa93353ed927a532ef5cb1e69d9061d)

(e) I haven't seen any adverse effects, so removing hasOwnProperty from the for..in loop improves performance)

james-fray commented 7 years ago

@kurtextrem can you prepare a pull request.

kurtextrem commented 7 years ago

Sorry for the late response, I was in holidays. Still want the PR? Although I'm not sure how I'd do that (new branch in my fork and then PR from there?)

james-fray commented 7 years ago

I've done a few optimizations based on your comments. Get the new code, and modify it if you still have ideas to improve the performance then request a pull merge.

https://help.github.com/articles/about-pull-requests/

sergeevabc commented 5 years ago

@james-fray, c’mmon, make this fork shine again!