DEV Community

Cover image for Why You Shouldn’t Use Libraries Blindly: A Lesson in Bundle Size
Ram kumar Shrestha
Ram kumar Shrestha

Posted on

Why You Shouldn’t Use Libraries Blindly: A Lesson in Bundle Size

When developing any web applications, the instinctive choice is to reach for popular libraries that solve our specific problem quickly. However, while libraries can save time, it’s essential to carefully evaluate their impact on the final product. One often overlooked factor is bundle size, which can significantly affect your application’s performance, especially for end-users with limited internet speed.

The Issue: Too Big Bundle Sizes

Each third-party library you add to a project contributes to the overall size of your application’s bundle. Large bundles can slow down page load times, leading to poor user experience and lower SEO rankings. This issue becomes more evident when libraries include more functionality than you need. Initially, you may think what if our bundle size increases by just some MB. But, that will cost a lot in page load time.

Real-World Example: The country-state-city Library

Let's say we’re building a simple form with dropdowns for selecting a user’s address. At first glance, country-state-city seems like an ideal choice — it’s popular, covers all the data you need, and is ready to go. However, on closer inspection, it brings along a hefty payload, as it includes an extensive database of countries, states, and cities worldwide all in frontend only.

For instance:

  • Bundle Bloat: country-state-city brings all three datasets: countries, states, and cities, and with a great cost its about 10MB+ in size which will be much more big than app itself.
  • Stale Data: While the country-state-city library offers a comprehensive dataset, it’s not always up-to-date. For example, in Nepal, administrative divisions have shifted from development regions to provinces. However, country-state-city still organizes Nepal’s data according to the outdated development regions. Relying on such a library can lead to inconsistencies, as users may encounter data that doesn’t align with current administrative structures. Loading this extra data could negatively affect your app’s performance, and ultimately, the user experience.

The Solution: Create a Custom API

Instead of using a library that doesn't meet client's expectations, consider creating a custom API that serves the data your application requires. In this example, building a backend API for this will completely remove the oversized library. Here are a few reasons why:

  • Reduced Bundle Size: Custom APIs removes the oversized library form frontend totally, reducing load times and improving responsiveness.
  • Better Performance: Custom APIs offer faster response times because they process smaller requests and return only the required information.
  • Enhanced Control: With a custom API, you’re in control of the data’s structure and format, allowing you to modify it as your app evolves.

Steps to Build Your Own API for Country and State Data

Here’s a quick outline for building your own API:

  • Define the Requirements: Identify the data scope—perhaps only a limited set of countries and states, the country-state-city itself can be used as great inspiration and reference.
  • Data Source: Use a reliable open-source dataset or an external API to populate your database.
  • Build the API: Create a simple REST API endpoint to serve this data.
  • Optimize for Performance: Cache frequently requested data and use efficient database queries.
  • Integrate with the App: Fetch only the data you need from this custom API, rather than an entire library.

Key Takeaway: Assess Before You Install

While it can be tempting to use libraries for their convenience, it’s essential to consider the implications carefully:

  • Evaluate Bundle Impact: Before adding a library, check the impact on bundle size.
  • Consider Long-Term Maintenance: Additional libraries introduce dependencies, which require updates and maintenance.
  • Check for Unused Features: Look for ways to use only the parts you need or alternatives to avoid excess data.

By taking the time to assess each library and evaluating alternative solutions like custom APIs, you can improve performance and ensure your app remains light and user-friendly. Libraries can be great tools, but they aren’t a one-size-fits-all solution—use them wisely to strike the right balance between convenience and efficiency.

Top comments (2)

Collapse
 
dorjelama profile image
Dorje Lama

I totally agree about evaluating library choices. I’ve had similar issues with libraries that seemed lightweight but ended up bloating my bundle. Recently, I worked on a project where we replaced a large data library with a custom API, and it made a noticeable difference in performance. I think it’s key to analyze dependencies thoroughly, especially for high-traffic apps!

Collapse
 
darkfox13 profile image
samuel eldridge

I really love this article. It is one of key aspec in any coding enviroment and language. Keep in min what you are trying to achive how each particulkar libary add to you project. Try to minimise the number of libaries you use.

I often think that somtime i would love to pull out key function of certain libaries becaus i feel like it would trim some fat of my projects. The main diffculty with this is that many libaries are eaither locked away or frragment making that process a heache.