<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: roverbober</title>
    <description>The latest articles on DEV Community by roverbober (@roverbober).</description>
    <link>https://dev.to/roverbober</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1241956%2F437ef2f7-263e-486a-82e8-ae267eb28972.jpg</url>
      <title>DEV Community: roverbober</title>
      <link>https://dev.to/roverbober</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/roverbober"/>
    <language>en</language>
    <item>
      <title>IndexedDB: Understanding Performance Pitfalls (Part 1)</title>
      <dc:creator>roverbober</dc:creator>
      <pubDate>Tue, 26 Dec 2023 17:39:41 +0000</pubDate>
      <link>https://dev.to/roverbober/indexeddb-understanding-performance-pitfalls-part-1-434d</link>
      <guid>https://dev.to/roverbober/indexeddb-understanding-performance-pitfalls-part-1-434d</guid>
      <description>&lt;p&gt;The main issue we’re addressing here is the long initialization time of IndexedDB.&lt;/p&gt;

&lt;p&gt;When you initiate a connection to a database, the code typically looks like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;window.indexedDB.open("TestDB", 1);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Normally, this process is quite fast, taking around 20 milliseconds — a pretty swift operation. However, during the first-time setup, it might take a bit longer because it involves creating the database from scratch.&lt;/p&gt;

&lt;p&gt;Feel free to try it out for yourself. Go to the &lt;a href="https://roverboberrr.github.io/indexeddb-perf-issues/src/"&gt;sandbox&lt;/a&gt; and take a look at the “Initialization Time” value.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--z0tvdqoi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/12gckogmzi71log2t4c8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--z0tvdqoi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/12gckogmzi71log2t4c8.png" alt="Image description" width="800" height="139"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After refreshing the page a few times, you’ll see that this value typically remains low.&lt;/p&gt;

&lt;p&gt;At the moment, IndexedDB is empty. It consists of 30 tables, but there’s no data in them. You can verify this by opening DevTools and checking the tables and their contents.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MG8fjqlT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4el8hhc839zgf7yssxux.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MG8fjqlT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4el8hhc839zgf7yssxux.png" alt="Image description" width="800" height="341"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s change this by adding some data. Simply click the “Add Items” button, and the script will start populating the tables.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TW8b4Rvt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x2yhbyrq5zqa53pt00j5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TW8b4Rvt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x2yhbyrq5zqa53pt00j5.png" alt="Image description" width="800" height="130"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Please wait until it’s finished. This will increase the overall size of IndexedDB to 379 Mb.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7pI2t-G9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pzo6t4jxhna4fla94u1f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7pI2t-G9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pzo6t4jxhna4fla94u1f.png" alt="Image description" width="800" height="377"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After refreshing the page, you may notice a slightly higher “Initialization Time” value — nothing to be too concerned about at this stage.&lt;/p&gt;

&lt;p&gt;However, try closing the tab and reopening it; this time, you might experience a noticeable delay, potentially up to 2 seconds. Keep in mind that the exact duration can vary depending on your device and browser. My tests were conducted on a Mac using Chrome.&lt;/p&gt;

&lt;p&gt;If you continue to add more data, this delay will become even longer.&lt;/p&gt;

&lt;p&gt;Making your users wait for a few seconds when they open your web application is not ideal. To address this issue, consider these tips:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Don’t block your UI rendering while waiting for IndexedDB. Opening a connection is an asynchronous operation, so load and display what you can to your users, and wait for data from IndexedDB in the background. Once it’s available, render the data.&lt;/li&gt;
&lt;li&gt;Review what you store in IndexedDB. If you’re storing files or media, think about using a Service Worker and Cache API. This approach can simplify the database structure, reduce its size, and enhance its performance.&lt;/li&gt;
&lt;li&gt;Consider splitting your database into multiple databases. For instance, if your application can be used by multiple users on the same device, create a separate database for each user. With a single database containing 800 megabytes of data, initialization time may be around 3 seconds. However, by dividing it into two databases, the initialization time can be reduced to 1.5 seconds.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>webdev</category>
      <category>pwa</category>
      <category>indexeddb</category>
      <category>performance</category>
    </item>
  </channel>
</rss>
