How To Use Indexed DB To Improve Page Load Performance

Introduction

Indexed DB is a local storage system which stores data in client-side on the browser. This is useful for applications that require storing a large amount of the data. These applications can run more efficiently and can load faster.

Objective

The main objective of this indexed DB is to improve the performance of the page load. It avoids the number of calls to retrieve the data from the database

How it works
  • First time when page loads, it calls the data from the database and stores the data in the indexed DB.
  • When again calling for the same record, it will load from the indexed DB instead of the load from the database. So, the application can run more efficiently and load faster.
  • Since the indexed DB gets updated on every call, it will have the updated data just same as in the database.
  • Since it displays the data from the indexed DB and then calls to the database on the page load, the application can improve the load performance.
Things to know before starting with Indexed DB
  • To start working with indexed DB, we must check whether our browser supports indexed DB or not.
  • We must be aware of the below methods.
    1. Push the data to indexed DB when data comes from the database for the first time.
    2. Pull data from the indexed DB when data is already existing in the indexed DB.
    3. Filter the data based on the requirement.
    4. Update the indexed DB based on the changes.
Step1 - Check browser compatibility

Check the browser compatibility by running the below code.

  1. window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;  
  2. window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction;  
  3. window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange  
  4. if (!window.indexedDB) {  
  5.     window.alert("Your browser doesn't support IndexedDB.")  
  6. }  

Step2

If the browser supports the indexed DB, write a code for adding the retrieved data to the indexed DB.

Code for adding the data into indexed DB.

  1. var dbName = "MyDB";  
  2. var request = indexedDB.open(dbName);  
  3. request.onerror = function(event) {  
  4.     // Handle errors.  
  5. };  
  6. request.onupgradeneeded = function(event) {  
  7.     db = event.target.result;  
  8.     objectStore = db.createObjectStore("Discussions", {  
  9.         keyPath: "ID"  
  10.     });  
  11.     objectStore.createIndex("ID""ID", {  
  12.         unique: false  
  13.     });  
  14.     objectStore.transaction.oncomplete = function(event) {  
  15.         customerObjectStore = db.transaction("Discussions""readwrite").objectStore("Discussions");  
  16.         for (var i in customerData) { /* where the customerData is our retrieved Data*/  
  17.             customerObjectStore.add(customerData[i]);  
  18.         }  
  19.     };  
  20. }; 
  • Then, call the functions in the success event of request, as shown below.
    1. request.onsuccess = function(evt) {  
    2.     db = evt.target.result;  
    3.     add(); /*this is to push the current data to the existing data in the indexed DB*/  
    4.     GetAll(); /* to get the all data fron the indexed dB*/  
    5.     CheckForCurrentData(DataFromIndexedDB); /* filter the particular record from the indexed DB*/  
    6.     updateResult(DiscID); /* update a record in the indexed DB*/  
    7.     Delete(DiscID); /* Delete a record in the indexed DB*/  

The mentioned functions are defined as following.

  • Code for pushing another retrieved data to the existing indexed DB data.
  • This is a call to push the SharePoint list data to indexed DB. By using the following code, you can push the data from SharePoint list and you can store it in your indexed DB.
    1. function add() {  
    2.     for (var i = 0; i < customerData.length; i++) {  
    3.         request = db.transaction(["Discussions"], "readwrite").objectStore("Discussions").add(customerData[i]);  
    4.     }  
    5. }  
  • Code to get the all the indexed DB data.
    1. function GetAll() {  
    2.     objectStore = db.transaction("Discussions").objectStore('Discussions').getAll();  
    3.     objectStore.onsuccess = function(event) {  
    4.         DataFromIndexedDB = event.target.result;  
    5.     }  
    6. }  
  • Code to get the particular ID data from the indexed DB.
    1. function CheckForCurrentData(DataFromIndexedDB) {  
    2.     var CurrentPageData = [];  
    3.     for (var i = 0; i < DataFromIndexedDB.length; i++) {  
    4.         if (DataFromIndexedDB[i].ParentId == RecordId) {  
    5.             CurrentPageData.push(DataFromIndexedDB[i]);  
    6.         }  
    7.     }  
  • Code to update a record in the indexed DB.
    1. function updateResult(DiscID) {  
    2.     console.log(DiscID);  
    3.     var transaction = db.transaction(['Discussions'], 'readwrite');  
    4.     var objectStore = transaction.objectStore('Discussions');  
    5.     objectStore.openCursor().onsuccess = function(event) {  
    6.         var cursor = event.target.result;  
    7.         console.log(cursor);  
    8.         if (cursor) {  
    9.             if (cursor.value.ID === DiscID) {  
    10.                 var updateData = cursor.value;  
    11.                 updateData.ReplyCount = updateData.ReplyCount + 1;  
    12.                 var request = cursor.update(updateData);  
    13.             };  
    14.         }  
    15.     };  
    16. }; 
  • Code to delete a record in the indexed DB
    1. function Delete(DiscID) {  
    2.     var request = db.transaction(["Discussions"], "readwrite").objectStore("Discussions").delete(DiscID);  
References for the above files

Please add the following JS file to your code to support some methods while using Internet Explorer.

  1. <script src=” https://github.com/dumbmatter/IndexedDB-getAll-shim”></script> 
Note

Here, I chose SharePoint as an environment and Angular JS as JS file to do crud operations on the SharePoint list. You can use your desired environment and JavaScripts to perform operations on the actual database, instead of SharePoint and AngularJS.

Conclusion

This article will help you improve the page load performance and the crud operations on the indexed DB.

Next Recommended Readings