Skip to content

Improve speed#24

Closed
JaeseokWoo wants to merge 3 commits intoAndrejGajdos:masterfrom
JaeseokWoo:performance-branch
Closed

Improve speed#24
JaeseokWoo wants to merge 3 commits intoAndrejGajdos:masterfrom
JaeseokWoo:performance-branch

Conversation

@JaeseokWoo
Copy link
Copy Markdown

@JaeseokWoo JaeseokWoo commented Dec 22, 2021

Hello
I learned link-preview-generator during the project.
After using it, I thought it was slow, so I looked at the source code.
Find something to improve.

Block unnecessary resources

await page.setRequestInterception(true);

 page.on('request', req => {
    switch (req.resourceType()) {
      case 'stylesheet':
      case 'font':
      case 'beacon':
      case 'media':
      case 'script':
        req.abort();
        break;
      default:
        req.continue();
    }
  });

Testing

Processor: Intel(R) Core(TM) i5-10210U CPU @ 1.60GHz 2.11 GHz
RAM: 8.00GB
OS: Windows 10 Home
node: v14.17.3
npm: 6.14.13

const improve = require('./improve');
const normal = require('./index');

const improveTest = async (url) => {
    console.time('test');
    const data = await improve(url);
    console.log(data);
    console.timeEnd('test');
}

const normalTest = async (url) => {
    console.time('test');
    const data = await normal(url);
    console.log(data);
    console.timeEnd('test');
}

const urls = [
    'https://www.google.co.kr/',
    'https://www.youtube.com/',
    'https://www.facebook.com/',
    'https://www.apple.com/kr/',
]

improveTest(urls[3]);
//normalTest(urls[3]);

Test Result

testURL https://www.google.co.kr/ (s): second
Improvement 2.562 2.449 2.674 2.466 2.592
Original 3.307 3.136 3.148 3.213 3.317
testURL https://www.youtube.com/ (s): second
Improvement 2.861 3.123 2.762 2.701 2.658
Original 6.393 6.259 6.481 6.183 6.646
testURL https://www.facebook.com/ (s): second
Improvement 2.4 2.368 2.244 2.388 2.339
Original 2.303 2.602 2.627 2.636 2.593
testURL https://www.apple.com/kr/ (s): second
Improvement 2.617 1.35 1.487 2.475 1.411
Original 2.316 2.339 2.375 2.237 2.296
There is no big difference, but I think it has improved overall.

finish

I want to help improve link-preview-generator performance.
I am currently a student, and I am not good at English.
That's why the above content is a translator.
Anyway, I would appreciate it if you could leave a feedback.
I like feedback.

request abort stylesheet, font, media , etc
it is necessary that the resource type is 'script'
@JaeseokWoo
Copy link
Copy Markdown
Author

I think it is necessary that the resource type is 'script'.
So I changed logic to block unneccesary resources and retested them.

Block unnecessary resources

await page.setRequestInterception(true);

 page.on('request', req => {
    switch (req.resourceType()) {
      case 'stylesheet':
      case 'font':
      case 'beacon':
      case 'media':
      case 'main_frame':
      case 'websocket':
      case 'sub_frame':
        req.abort();
        break;
      default:
        req.continue();
    }
  });

Test Result

testURL https://www.google.co.kr/ avg
Improvement 2.936 2.927 3.291 2.893 3.279 3.166 3.023 2.916 3.642 3.165 3.1238
Original 3.012 3.615 3.104 3.015 3.108 3.426 3.352 3.131 3.189 2.827 3.1779
testURL https://www.youtube.com/ avg
Improvement 5.923 5.231 5.138 5.279 5.517 5.306 5.268 5.269 5.644 5.918 5.4493
Original 6.214 5.943 6.074 5.511 6.47 5.359 5.945 5.584 5.653 5.512 5.8265
testURL https://www.facebook.com/ avg
Improvement 3.101 3.085 3.032 2.993 2.976 3.269 2.953 2.97 3.131 2.972 3.0482
Original 3.356 3.175 3.598 3.01 3.179 3.223 3.129 3.359 3.117 3.028 3.2174
testURL https://www.apple.com/kr/ avg
Improvement 2.14 2.215 2.065 2.574 2.108 2.141 2.226 2.097 2.326 2.059 2.1951
Original 3.197 3.067 3.048 2.806 2.925 3.051 3.101 3.127 2.557 2.704 2.9583
The performance has improved a little.😅

@AndrejGajdos
Copy link
Copy Markdown
Owner

@JaeseokWoo that looks really good. Sorry I didn't check this sooner. I was too busy. Can you please remove your packge-lock.json from your PR? I would like to keep "lockfileVersion": 2 in this library.

to keep "lockfileVersion": 2 in this library.
@JaeseokWoo
Copy link
Copy Markdown
Author

@AndrejGajdos Thank you for the reply.
I will close this PR and send another one to keep "lockfileVersion": 2.

This pull request was closed.
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