2

I have an application where I try to save a user's favorites in the indexedDB through the use of the dexie.js wrapper. I have little experience with the indexedDB, but the performance I'm seeing seems to be... not very good.

My table is

this.version(3).stores({
  shows: 'id'
});

I wrote the following function to update the record when it exists or to create it when it doesn't:

updateFavorite(showId: number) {
  const t1 = performance.now();
  this.table('shows').get(showId).then(
    (found?: ShowDb) => {
      const t2 = performance.now();
      if (found === undefined) {
        this.table('shows').add({id: showId, favorite: true}).then(
          () => {
            const t3 = performance.now();
            console.log(`GET query took ${t2 - t1}`);
            console.log(`ADD took ${t3 - t2}`)
          }
        )
      } else {
        this.table('shows').update(found.id, {favorite: !found.favorite}).then(
          () => {
            const t3 = performance.now();
            console.log(`GET query took ${t2 - t1}`);
            console.log(`UPDATE took ${t3 - t2}`)
          }
        );
      }
    }
  )
}

The table contains about 50 records. The GET query averages around 100ms, while the UPDATE takes around 170ms. I tried to add an await db.open() to make sure the database connection was open, but this didn't make any difference. Are these performance numbers to be expected for getting a record by it's primary key and inserting one record or am I doing something wrong?

EDIT: I made a quick function that loops the dexie table.get query 100 times and did the exact same thing for the equivalent raw indexedDB query. The Dexie query avaraged 90ms, while the indexedDB one averaged 40ms. That's still more than what I would expect from a local small datastore, but a pretty significant improvement.

vixducis
  • 1,010
  • 1
  • 8
  • 22

0 Answers0