Podcast Lookup

StoicCodeCast 2 - Loading JS scripts asynchronously

StoicCodeCast

  • November 28, 2024
  • 12 min
Episode page
About this episodeEpisode #2, 2024-11-28 Let’s learn about different ways JavaScript code can be loaded onto a HTML page! Follow along the source article…

Episode #2, 2024-11-28

Let’s learn about different ways JavaScript code can be loaded onto a HTML page!

Follow along the source article here.

Check out our published Python book.

The best way to send money abroad or have virtual cards: create a wise account and select a bonus!

Support our work.



Get full access to Stoic Coding at costa86.substack.com/subscribe

Transcript

From the publisher

This transcript comes from the podcast’s own feed, shown as the publisher wrote it.

StoicCodeCast 2 - Loading JS scripts asynchronously

00:00—
  1. Speaker 1

    Hey, everyone. I'm really excited about today's deep dive. It's all about JavaScript and how it gets loaded on websites.

  2. Speaker 2

    Sounds pretty technical. But I bet you've got some interesting stuff lined up.

  3. Speaker 1

    Oh, absolutely.

  4. Speaker 2

    Yeah.

  5. Speaker 1

    We're going way beyond the basics here.

  6. Speaker 2

    Yeah.

  7. Speaker 1

    You know, we've got a couple of articles someone sent in. And even a cool diagram really gets into the nuts and bolts of how browsers handle all that JavaScript code.

  8. Speaker 2

    Awesome. I'm ready to get technical.

  9. Speaker 1

    Me too.

  10. Speaker 2

    Yeah.

  11. Speaker 1

    But you know what really caught my eye? It's not just about the different ways JavaScript can be loaded. It's about the impact. I mean, who hasn't been stuck staring at a blank screen waiting for a website to finally load?

  12. Speaker 2

    Yeah, the worst. And in today's world, people are so impatient.

  13. Speaker 1

    Exactly. I read somewhere that Google actually penalizes slow loading websites in their search rankings.

  14. Speaker 2

    Makes sense. No one wants to wait around forever.

  15. Speaker 1

    Right. So let's get into it. What's the big deal with JavaScript loading anyway? It's just for adding a little interactivity, right?

  16. Speaker 2

    Well, that's where things get interesting. See, the way the JavaScript is loaded can either make your website lightning fast or, well, let's just say it not so fast.

  17. Speaker 1

    OK, so I'm sensing there's more to this than meets the eye.

  18. Speaker 2

    Definitely. Let's start with the most common way JavaScript is loaded. It's called synchronous loading. And it's kind of like imagine a chef following a recipe step by step. That recipe is the HTML code of a website.

  19. Speaker 1

    OK, I'm following you. So the chef is the browser.

  20. Speaker 2

    Exactly. So the chef, our browser, is going through the recipe, the HTML, and then Bam! It hits a line that says, go get the JavaScript.

  21. Speaker 1

    Uh-oh. Sounds like trouble.

  22. Speaker 2

    It can be because what happens is everything stops. The chef puts down their tools, they stop chomping those veggies, everything, all to go fetch that JavaScript.

  23. Speaker 1

    So the whole recipe, the whole website just grinds to a halt.

  24. Speaker 2

    You got it. And meanwhile, your users are staring at an empty plate or a blank screen.

  25. Speaker 1

    That's where that diagram comes in handy.

  26. Speaker 2

    I know it. It shows exactly how the browser pauses at each step to deal with the JavaScript.

  27. Speaker 1

    Yikes. So what do we do? How do we get our chef back to cooking and keep those users happy?

  28. Speaker 2

    There are a few tricks up our sleeve. One way is to use something called the async attribute. It's basically like telling the browser, hey, go ahead and download that JavaScript in the background while you keep working on the rest of the HTML.

  29. Speaker 1

    Ooh, I like that. So it's like this chef sending their sous chef to grab that ingredient.

  30. Speaker 2

    Exactly. Things keep moving, no more stopping the whole show.

  31. Speaker 1

    Okay, so problem solved, right? Async for the win.

  32. Speaker 2

    Well, hold on. It's definitely a step in the right direction, but there's a bit more to the story.

  33. Speaker 1

    Oh, I knew it couldn't be that simple.

  34. Speaker 2

    So async does allow the script to download in the background. But here's the thing. The browser still stops everything to actually run the script once it's ready.

  35. Speaker 1

    Hmm. So it's still a bit of a bottleneck.

  36. Speaker 2

    It can be. It's great for some things, like those non-critical elements you might see on a web page.

  37. Speaker 1

    Like ads.

  38. Speaker 2

    Exactly. Things that aren't essential to the core functionality of the website. But for those important scripts, we need a more elegant solution.

  39. Speaker 1

    Okay, I'm intrigued. Let's talk about that more elegant solution then.

  40. Speaker 2

    Let's do it. And that's where the defer attribute comes in. Our hero of the JavaScript loading world. Oh, defer.

  41. Speaker 1

    So what makes defer so heroic?

  42. Speaker 2

    Oh, remember how we were talking about async being like that sous chef grabbing the ingredient, but then interrupting the chef. The second they get back defer is different. It's like saying, Hey, go grab that ingredient, but wait until I'm actually ready for it. Don't just barge in and disrupt the whole flow.

  43. Speaker 1

    I'm starting to see how this is more elegant.

  44. Speaker 2

    Right. So with defer, the script still downloads in the background, but it patiently waits for all the HTML to be parsed. You know, like the chef carefully reading through the entire recipe before starting to cook.

  45. Speaker 1

    Ah, that makes sense. So no more interruptions. The browser can focus on getting that initial page content ready for the user as quickly as possible.

  46. Speaker 2

    Exactly. And you know, there's another cool thing about defer. Oh, do you remember that diagram we were looking at with all those different JavaScript files?

  47. Speaker 1

    Yeah. Some of those files were pretty big.

  48. Speaker 2

    Uh, Exactly. And, you know, it's kind of like a pack of wolves. The bigger, older wolves, they need a little more time to get moving. They can't just sprint off like the younger ones.

  49. Speaker 1

    OK, I'm liking this analogy. The bigger JavaScript files are the older wolves.

  50. Speaker 2

    You got it. And defer. It's like the alpha wolf. Making sure everyone stays in formation. The bigger files get loaded first, giving them plenty of time to download without holding up the rest of the pack.

  51. Speaker 1

    That's a great way to visualize it. So if we want a website to load super fast, are we just slapping defer on every JavaScript file we can find?

  52. Speaker 2

    Well, not necessarily. It's not a magic bullet. You know, it really depends on the specific website and the purpose of each script.

  53. Speaker 1

    OK, so it's not as simple as just saying defer is always the best.

  54. Speaker 2

    Right. It's more about strategy. Think of it like this. Let's say you're building an e-commerce website.

  55. Speaker 1

    OK, so lots of products, maybe some images, possibly some animations to make things look fancy.

  56. Speaker 2

    Exactly. Now, for that kind of website, what's the most important thing?

  57. Speaker 1

    Hmm. Well, I guess you'd want people to be able to browse products and add them to their cart without any hiccups.

  58. Speaker 2

    Exactly. That's the core experience. So for scripts that handle things like product displays or the shopping cart functionality defer would be your best friend.

  59. Speaker 1

    Makes sense. You don't want people abandoning their carts because the add to cart button is taking forever to respond.

  60. Speaker 2

    Absolutely. But what about those features that aren't essential for the initial page load? You know, things that enhance the experience but aren't absolutely crucial right off the bat.

  61. Speaker 1

    Like what?

  62. Speaker 2

    Think about something like a size chart that pops up when you click a button.

  63. Speaker 1

    Yeah.

  64. Speaker 2

    Or maybe a section with product recommendations that loads further down the page. Yeah.

  65. Speaker 1

    Okay, I see what you mean. Stuff that's nice to have, but not a deal breaker if it takes a second to load.

  66. Speaker 2

    Exactly. And for those kinds of features, we've got another trick up our sleeve. It's called dynamic loading.

  67. Speaker 1

    Dynamic loading. Okay, this is where it gets really fancy, right?

  68. Speaker 2

    You could say that. It's all about loading scripts on demand. So instead of loading every single script up front, you only fetch and run the script when it's actually needed.

  69. Speaker 1

    So going back to our e-commerce example, that size chart script, it wouldn't even download until someone actually clicks the button to view it.

  70. Speaker 2

    Precisely, which means the initial page load is super fast because it's not bogged down with all those extra scripts.

  71. Speaker 1

    I'm seeing the appeal here. So dynamic loading helps improve that first impression, that initial page load that can make or break a user's experience.

  72. Speaker 2

    It's all about being strategic, you know, choosing the right tool for the job. Sometimes it's defer, sometimes it's dynamic loading. And yeah, sometimes even that good old fashioned synchronous loading still has its place.

  73. Speaker 1

    So there's no one-size-fits-all approach when it comes to JavaScript loading?

  74. Speaker 2

    Nope. You've got to think about the purpose of the website, users' expectations, and then carefully choose the best way to load those scripts.

  75. Speaker 1

    This is fascinating. I'm really starting to appreciate the complexity involved in something that I'll admit I used to take for granted.

  76. Speaker 2

    It's like a hidden world of optimization behind every website we visit.

  77. Speaker 1

    It really is.

  78. Speaker 2

    Yeah.

  79. Speaker 1

    But all this talk about optimization, it makes me wonder, Are there any downsides? I mean, is there ever a reason to not use these techniques to just stick with that traditional synchronous loading?

  80. Speaker 2

    That's a great question. And, you know, while these techniques are super powerful, there are definitely situations where they might not be the best choice.

  81. Speaker 1

    Oh, like what?

  82. Speaker 2

    Well, imagine a script that's super tiny, just a few lines of code, and it's absolutely crucial for the website's core functionality.

  83. Speaker 1

    So it needs to run before anything else can happen.

  84. Speaker 2

    Exactly. In that case, synchronous loading might be the simplest and most efficient way to go.

  85. Speaker 1

    Okay, so if it's not going to cause a noticeable delay and it's essential for the website to function properly, sticking with the traditional method might actually make sense.

  86. Speaker 2

    Right. It's all about finding that balance. And, you know, we also have to keep in mind that not all browsers are created equal.

  87. Speaker 1

    Oh, right. Some people might still be using older browsers.

  88. Speaker 2

    Exactly. And some of those older browsers might not fully support asynchronous or dynamic loading. So in those cases, it's important to make sure things work smoothly for everyone, even if it means sticking with synchronous loading for certain scripts.

  89. Speaker 1

    It's amazing how much thought goes into something that seems so simple on the surface.

  90. Speaker 2

    Isn't it? There's so much happening behind the scenes to make sure that websites load quickly and efficiently.

  91. Speaker 1

    Well, I'm definitely feeling more enlightened now.

  92. Speaker 2

    Me too. It's always fun to dive deep into these technical details and see how they impact the user experience.

  93. Speaker 1

    Wow. So we've really gone down the rabbit hole on JavaScript loading, haven't we?

  94. Speaker 2

    We have, from async and defer to dynamic loading and even those edge cases where the good old synchronous approach still reigns supreme.

  95. Speaker 1

    It's amazing how much complexity there is in something that, you know, most people probably never even think about.

  96. Speaker 2

    It's true. But, you know, all this complexity boils down to one crucial goal, giving users a smooth and enjoyable experience. Because, let's face it, nobody wants to wait around for a page to load. And we've definitely established that Google doesn't reward slow websites either.

  97. Speaker 1

    Right. Speed is king. And a slow website is just, it's just frustrating.

  98. Speaker 2

    Absolutely. It's like trying to have a conversation with someone who takes forever to respond.

  99. Speaker 1

    Yeah. So let's recap. What have we learned about this hidden world of JavaScript loading?

  100. Speaker 2

    Well, we started with synchronous loading. The browser stops everything, fitches the JavaScript, runs it, and then finally continues on with the rest of the HTML.

  101. Speaker 1

    Right, and that can lead to some serious delays, especially if those JavaScript files were on the larger side.

  102. Speaker 2

    Then we talked about asynchronous loading, where the browser can download those scripts in the background without turning everything to a screeching halt.

  103. Speaker 1

    And we explored those two key players, async and defer.

  104. Speaker 2

    Async being great for those non-critical scripts. And defer. Well, defer is like that polite guest at a party waiting for the right moment to join the conversation.

  105. Speaker 1

    Exactly. No interrupting the flow. And that Wolfpack analogy, that was a great way to visualize how defer helps those larger scripts load without holding up the rest of the page.

  106. Speaker 2

    Glad you liked it. And of course, we can't forget about dynamic loading, only loading those scripts when they're actually needed.

  107. Speaker 1

    Which can be a real game changer when it comes to initial page load time.

  108. Speaker 2

    Absolutely. And it can even help reduce the strain on the user's internet connection, which is always a good thing.

  109. Speaker 1

    It really makes you appreciate all the effort that goes into optimizing a website for speed and performance.

  110. Speaker 2

    It does. It's a constant balancing act between functionality and efficiency. And it's amazing how much progress has been made in recent years.

  111. Speaker 1

    Yeah. I mean, we even touched upon the idea of websites that can predict what scripts will need and preload them for us. Almost like they're reading our minds.

  112. Speaker 2

    It's incredible. Right. And who knows what the future holds. Maybe one day websites will be so personalized that they'll adapt to our individual browsing habits and preferences in real time.

  113. Speaker 1

    That would be amazing. It's exciting to think about the possibilities.

  114. Speaker 2

    Definitely. But, you know, thinking about all of this. It makes me wonder, what if users had even more control over how JavaScript is loaded on the websites they visit?

  115. Speaker 1

    Hmm, that's an interesting thought.

  116. Speaker 2

    What do you mean?

  117. Speaker 1

    Imagine if there were browser extensions or settings that let users customize their JavaScript loading preferences.

  118. Speaker 2

    So we could choose to prioritize speed over functionality. Or vice versa.

  119. Speaker 1

    Exactly. It would be like having a personalized control panel for JavaScript loading.

  120. Speaker 2

    I like it. So if I'm all about speed, I could choose to block all those non-essential scripts. But if I want the full bells and whistles experience, I could opt for that, even if it means a slightly slower load time.

  121. Speaker 1

    Right. It would be about giving users more agency, more control over their online experience.

  122. Speaker 2

    I love that idea. It's like having a dialogue with the website, telling it how we want it to behave.

  123. Speaker 1

    It's a fascinating club zone, and it really speaks to the evolving relationship between web developers and users. We're moving towards a more collaborative and personalized web. Well said. I think that's a perfect note to end on. We've really covered a lot of ground today, from the nitty-gritty of async and defer to the exciting possibilities of dynamic loading and user-controlled JavaScript preferences.

  124. Speaker 2

    It's been a great deep dive. And remember, the world of web development is constantly evolving. So stay curious and keep exploring.

  125. Speaker 1

    Absolutely. And until next time, happy browsing, everyone.