IndexedDB vs localStorage

JavaScript performance comparison

Revision 13 of this test case created

Info

This tests localStorage performance against IndexedDB, both acting as an object store. localStorage tests contain JSON stringify/parse methods.

There's a performance penalty for the IDB tests though, as these operate on IDB via a wrapper, so there are are some additional function calls being made.

Preparation code

<script>
  var small, large, idb, websql, ls;

  // data setup
  small = 'test';
  large = '';
  for (var i = 0; i < 10000; i++) large += '1234567890';

  // store setup
  window.indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB;
  var request = indexedDB.open("benchmark");
  request.onsuccess = function(e) {
    var v = "1.0";
    idb = e.target.result;
    if (v != idb.version) {
      var setVrequest = idb.setVersion(v);
      setVrequest.onsuccess = function(e) {
        var store = idb.createObjectStore("benchmark");
        e.target.transaction.oncomplete = function() {
          var trans = idb.transaction(["benchmark"], "readwrite");
          var store = trans.objectStore("benchmark");
          store.add(small, 1);
          store.add(large, 2);
        };
      };
    };
  };

  ls = window.localStorage;
  ls.clear();
  ls[1] = small;
  ls[2] = large;

  websql = openDatabase('benchmark', '1.0', '', 1 * 1024 * 1024);
  websql.transaction(function(tx) {
    tx.executeSql('DROP TABLE IF EXISTS benchmark;');
    tx.executeSql('CREATE TABLE benchmark (id, value);');
    tx.executeSql('INSERT INTO benchmark (id, value) VALUES (?, ?)', [1, small]);
    tx.executeSql('INSERT INTO benchmark (id, value) VALUES (?, ?)', [2, large]);
  });
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
localStorage read small data
var value = ls[1];
pending…
localStorage read large data
var value = ls[2];
pending…
IDB read small data
// async test
idb.transaction("benchmark").objectStore("benchmark").get(1).onsuccess = function(event) {
  deferred.resolve();
};
pending…
IDB read large data
// async test
idb.transaction("benchmark").objectStore("benchmark").get(2).onsuccess = function(event) {
  deferred.resolve();
};
pending…
WebSQL read small data
// async test
websql.transaction(function(tx) {
  tx.executeSql('SELECT * FROM benchmark WHERE id=?', [1], function(t1, r1) {
    deferred.resolve();
  });
});
pending…
WebSQL read large data
// async test
websql.transaction(function(tx) {
  tx.executeSql('SELECT * FROM benchmark WHERE id=?', [2], function(t1, r1) {
    deferred.resolve();
  });
});
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

1 comment

; commented :

l

Add a comment