local-cdn icon indicating copy to clipboard operation
local-cdn copied to clipboard

Optimize the code

Open kurtextrem opened this issue 8 years ago • 4 comments

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)

kurtextrem avatar Feb 21 '17 09:02 kurtextrem

@kurtextrem can you prepare a pull request.

james-fray avatar Feb 21 '17 12:02 james-fray

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?)

kurtextrem avatar Mar 06 '17 08:03 kurtextrem

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/

james-fray avatar Mar 06 '17 10:03 james-fray

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

sergeevabc avatar Dec 12 '18 16:12 sergeevabc