Skip to content

Instantly share code, notes, and snippets.

@nolanlawson
Created March 16, 2018 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nolanlawson/b833509a2c02db3d7d44a6e61f65be5e to your computer and use it in GitHub Desktop.
Save nolanlawson/b833509a2c02db3d7d44a6e61f65be5e to your computer and use it in GitHub Desktop.
IDB cursor bench
<!doctype html>
<html>
<body>
Test Insersion of 10,000 customers: <label id='lblAddTime'></label><br>
Test retrieval of all customers: <label id='lblGetTime'></label><br>
<script src="script.js"></script>
</body>
</html>
var _currentVer = 1;
function _openDatabase(fnSuccess) {
var _custDb = window.indexedDB.open("MyDatabase", _currentVer);
_custDb.onsuccess = function (event) {
var db = event.target.result;
fnSuccess(db);
}
_custDb.onerror = function (event) {
_custDb = null;
fnSuccess(null); // should use localData
}
_custDb.onupgradeneeded = function (event) {
var db = event.target.result;
var txn = event.target.transaction;
// Create an objectStore for this database
if (event.oldVersion < _currentVer) {
var customer = db.createObjectStore("customer", { keyPath: "guid" });
var index = customer.createIndex("by_id", "id", { unique: false });
}
};
}
function _storeCustomers(list, fnSuccess) {
_openDatabase(function (db) {
if (db == null)
{
alert("not supported");
return;
}
var customerObjectStore = db.transaction("customer", "readwrite").objectStore("customer");
for (var i=0; i < list.length; i++) {
var request = customerObjectStore.put(list[i]);
}
customerObjectStore.transaction.oncomplete = function (event) {
console.log("completed");
fnSuccess(list.length);
}
});
}
function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
function makerandom(len) {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < len; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
function _retrieveCustomers(fn) {
_openDatabase(function (db) {
if (db == null)
{
alert("not supported");
return;
}
var customers = [];
var transaction = db.transaction("customer", "readonly");
var objectStore = transaction.objectStore("customer");
if (typeof objectStore.getAll === 'function') {
console.log("using getAll");
objectStore.getAll().onsuccess = function (event) {
fn(event.target.result);
};
}
else {
console.log("using openCursor");
objectStore.openCursor().onsuccess = function (event) {
var cursor = event.target.result;
if (cursor) {
customers.push(cursor.value);
cursor.continue();
}
else {
fn(customers);
}
};
}
});
}
function getCustomers() {
var el = document.getElementById('lblGetTime');
el.innerHTML = "retrieving";
var t0 = performance.now();
performance.mark('start getCustomers()')
_retrieveCustomers(function() {
var t1 = performance.now();
performance.mark('end getCustomers()')
el.innerHTML = "Get Time:" + (t1-t0) + "ms";
});
}
function addCustomers() {
var el = document.getElementById('lblAddTime');
el.innerHTML = "creating";
//
// create 10,000 random customers.
var list = [];
for( var i=1; i <= 10000; i++ ) {
list.push( {
'guid': uuidv4(),
'id': i,
'name': "Guido Customer",
'phone': "(206) 555-1212",
'otherdata': makerandom(10 + Math.random()*100)
});
}
el.innerHTML = "adding";
var t0 = performance.now();
performance.mark('start addCustomers()')
_storeCustomers(list, function() {
var t1 = performance.now();
performance.mark('end addCustomers()')
el.innerHTML = "Add Time:" + (t1-t0) + "ms";
// get our customers next
setTimeout( getCustomers, 100 );
});
}
addCustomers();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment