How can I "Reduce JavaScript execution time" of external scripts?

21,303

You won't have any control over what those external scripts do. Short of not including them, about the only thing you can do is defer their loading.

One way to do this is with the defer attribute.

<script defer src="https://example.com/script.js"></script>

This allows the page to continue to load and execute while the script is loaded and executed later. This method doesn't work with all scripts, but it will work with most. Many of the script tags you include from providers, such as Facebook, will already have alternative code which defers their loading.

See also: https://flaviocopes.com/javascript-async-defer/

Share:
21,303

Related videos on Youtube

InsuredApple
Author by

InsuredApple

Updated on February 05, 2020

Comments

  • InsuredApple
    InsuredApple over 4 years

    I have the feeling that there is not much I can do, but I thought I'd ask the question and see if I missed something.

    How can I "Reduce JavaScript execution time" of external scripts?

    When I use the PageSpeed Insights tool, my current score is 56, and one of the biggest things seems to be a problem with the execution time of some external scripts. And their caching... but that's different issue.

    https://load.sumo.com Total: 733 ms Script Evaluation: 587 ms Script Parse: 128 ms

    https://sumo.b-cdn.net/virtual/####/client/js/services/services.js Total: 436 ms Script Evaluation: 212 ms Script Parse: 90 ms

    https://connect.facebook.net/signals/config/###?v=2.8.33&r=stable Total: 215 ms Script Evaluation: 160 ms Script Parse: 53 ms

    https://static.leadpages.net/leadboxes/current/embed.js Total: 133 ms Script Evaluation: 123 ms Script Parse: 10 ms

    ...

    • Matthew Herbst
      Matthew Herbst over 5 years
      Where are you loading the scripts? if you are loading them in the page header then they are blocking and that's a big problem. If you are loading them in the body there are optimizations that can be made, such as loading them in parallel and pre-fetching. But you are correct that there is no way to make the script do less work. All you can do is optimize when that work is done, or, remove the script and not do the work at all.
    • InsuredApple
      InsuredApple over 5 years
      The scripts are loaded "correctly". Now I am just trying to understand if there is anything I can do about the JavaScript execution time, but it doesn't sound like it.
    • Matthew Herbst
      Matthew Herbst over 5 years
      I'm not doubting that they are loading correctly. I'm saying that depending on how you load them, you can optimize how long they take to parse and execute. Parsing of the scripts is a big part of that score and can cause the user lag if it takes too long and isn't handled properly. Again, I would suggest you to please show us how you are loading the scripts, and we might be able to help you greatly reduce how much of a performance impact they have on the page, which is what it sounds like you really care about.
  • InsuredApple
    InsuredApple over 5 years
    Thanks Brad. I've got the scripts deferred already, so that's good. I suppose that's the cost of having the functionality then.
  • Brad
    Brad over 5 years
    @InsuredApple Yes. If you are a big enough fish, you can try to go yell at the people who made the scripts and get them to optimize things. Chances are though, they're already looking at the load and execution times and have optimized them quite a bit. I know for sure that Facebook is always working on optimizing their SDKs, for example.