The Debate: Single Javascript File vs. Lazy Loading

Imagine you're working on a massive javascript-powered web application with minimal page refreshes in mind, containing around 80-100MB of unminified javascript to give an idea of its scale.

The theory is that by lazy-loading your javascript files, you can strike a better balance in load times, eliminating the wait time during page refreshes. In this scenario, lazy-loading may be more preferable than simply using a single minified .js file.

In theory, there's a fixed cost for requesting any file from a server regardless of its size. Therefore, excessive requests are not ideal. For instance, if a small javascript file loads alongside 10 other similarly sized files, it might be more efficient to group them together and save on multiple request costs.

So here's my query: Assuming reasonable defaults (let's say the client has a 3-5Mbps connection and decent hardware), what would be the optimal size for a file to request? Go too big, and you risk overloading the system; go too small, and the request cost might exceed the data being received, impacting data per second efficiency.

Edit: All the responses were great, but I picked Ben's as he provided a specific number.

Answer №2

To enhance user experience, I suggest keeping the initial loading size of a page below 300K to ensure quick display of content or at least a loading indicator. Subsequently, additional data can be fetched in chunks of up to 5MB at a time with a progress bar indicating the process. In my experience, large downloads exceeding 15MB have failed on coffee shop wifi networks, even when the connection seemed stable. If downloads above 5MB fail, it may not be fair to blame the website for potential issues.

Another approach is to download two files simultaneously after the initial <300k file using tools like LabJS or HeadJS to dynamically add script tags.

Answer №3

It's evident that requiring the client to download over a megabyte of JavaScript before any action can be taken is detrimental. Similarly, unnecessary downloads should also be avoided. However, there are undeniable advantages to having all resources cached.

Factors that may impact these considerations include:

  1. Round-trip time
  2. Server response time
  3. Header Size (including cookies)
  4. Caching Technique
  5. Browser (visit )

Maintaining a balance between parallel downloading and diverse cache requirements is crucial. Kyle Simpson recently touched on this topic here: http://www.reddit.com/r/javascript/comments/j7ng4/do_you_use_script_loaders/c29wza8

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Avoid adding any empty entries to the list using jQuery

I have implemented a code to prevent any blank entries from being added to my list, and it seems to be working fine. However, I can't shake the feeling that there might be a better way to achieve this. Even though it functions correctly, it doesn&apos ...

What is the best way to retrieve location data in a React application?

In my React project, I am trying to retrieve location information using the Geolocation API. To accomplish this task, I have included a button in the code below: import React, { Component } from 'react' class App extends Component { construc ...

The error message from the mongoose plugin is indicating a problem: it seems that the Schema for the model "Appointment" has not been properly registered

I need help troubleshooting a mongoose error that is being thrown. throw new mongoose.Error.MissingSchemaError(name); ^ MissingSchemaError: Schema hasn't been registered for model "Appointment". Use mongoose.model(name, schema) I have double-c ...

Ways to connect a click event to a dynamically generated child element with the help of jQuery?

I am aware that similar questions have been asked elsewhere, but as someone new to jQuery, I am still struggling to attach a click listener to an a element within a dynamically appended ul.li.a structure in the DOM. Below is an example of how the structure ...

Why does my console refuse to log the input entered for the search?

Looking to become proficient in web development, I am attempting to record HTML search queries in the console after storing them in a variable. However, when I try running the search procedure, nothing seems to be displaying in my browser's inspect co ...

What is the reason behind the for of loop breaking within an async function instead of properly awaiting the execution?

Update 2: I made changes to use setTimeOut instead, which fixed the issue. Check out the accepted answer for details on what was causing the error. The code below is now functioning properly. async function getSlices() { const imgBuffs = await sliceImg ...

Incorporate 3 additional compound filters with shuffle.js

Is there a way to add a third compound filter to the existing shuffle.js code provided below? // ES7 will have Array.prototype.includes. function arrayIncludes(array, value) { return array.indexOf(value) !== -1; } // Convert an array-like object to a r ...

Having difficulty with delaying the loading of a div layer and javascript after the page has initially loaded

I've been struggling to make this script wait a few seconds after the page has loaded, but I haven't been successful so far. Despite trying some solutions from similar questions here, nothing seems to be working. Any help you can provide would b ...

Traveling within a layered object

Currently, I'm working with an object and iterating through it to retrieve outputs as shown below: var obj = { "first": { "Bob": { "1": "foo", "2": "bar" }, "Jim": { "1": "baz" } }, "second": { "Bob": { ...

Is it possible to create a multi-page single-page application using Vue js SSR?

It may appear contradictory, but I struggle to find a better way to express it. When using vue server-side rendering, it seems you are limited to single page applications. However, for various reasons, I require an application with multiple real server-s ...

Tips for clearing text inputs in ReactJS

In my current Reactjs project using nextjs, I am facing an issue with clearing input fields after a click event. Below is the code snippet that I have been working on: <form onSubmit={handleSubmit}> <input type="text" ...

Access PDF files with parameters using ASP.NET MVC and AJAX technology

Currently, I am utilizing ASP.NET MVC and have created a controller that is able to return a PDF file. The PDF is constructed using PDFsharp: public ActionResult GenerateReport(string Param) { // Create a new PDF document PdfDocument document = n ...

What is the best way to manage the POST method in NextJS?

I am currently delving into the realms of NextJs(TS), Prisma, and MySQL with the aim to implement my learnings in ReactJS. However, I seem to be encountering some difficulties when attempting to make a POST call. Here's an overview of my project dire ...

Tips for making a dropdown menu within an Ionic Side menu

Looking for guidance on creating a sub menu in the ionic framework? I'm new to AngularJS and ionic framework, but eager to learn. Below is the code snippet I used to create a dropdown list component: <ion-side-menu side="left"> <ion-co ...

What triggers the onmouseout event to occur?

Is the event triggered continuously whenever the mouse is not hovering over the element? Or is it a one-time action when the mouse exits the element? This distinction is crucial for me to determine when the mouse pointer leaves the element, while only wa ...

Utilizing a Meteor Method within a Promise Handler [Halting without Throwing an Error]

I've been working on integrating the Gumroad-API NPM package into my Meteor app, but I've run into some server-side issues. Specifically, when attempting to make a Meteor method call or insert data into a collection within a Promise callback. Be ...

Concealing a block from top to bottom

I currently have a filter that is hidden when clicked, with a transition effect that moves it from the bottom to the top. I have recorded my screen to demonstrate this behavior: . However, I now need the filter to hide from top to bottom instead, essenti ...

Implement Dynamic Filters in a Vue Component

How can filters be programmatically applied to select values? During each iteration of records and columns, I am checking if the column ID starts with 'd', indicating it's a datetime field. In such cases, I want to apply the humanize filter ...

Error Unhandled in Node.js Application

I have encountered an issue in my NodeJS application where I have unhandled code in the data layer connecting to the database. I deliberately generate an error in the code but do not catch it. Here is an example: AdminRoleData.prototype.getRoleByRoleId = ...

What is the best way to compare and identify the variances between two JSON documents in Marklogic using JavaScript?

Is there a way to analyze and compare two JSON documents in Marklogic using JavaScript to identify any differences between them? ...