Skip to content

Instantly share code, notes, and snippets.

@ctoph3r
Created November 22, 2012 10:35
Show Gist options
  • Save ctoph3r/4130468 to your computer and use it in GitHub Desktop.
Save ctoph3r/4130468 to your computer and use it in GitHub Desktop.
indexedDB size tester
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script type = "text/javascript" src ="http://axemclion.github.com/IndexedDBShim/dist/IndexedDBShim.min.js"></script>
<script>
var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;
var IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction;
var IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange;
var db;
var txt = "";
function generateText(){
if(txt==""){
var sizeOfRecords = document.getElementById('sizeOfRecords').value;
for(i=0; i<parseInt(sizeOfRecords,10)*1000;i++){
txt+='l';;
}
return txt;
}
return txt;
}
function executeTest(){
var numOfRecord = document.getElementById('numOfRecord').value;
var records = new Array();
for(j=0;j<parseInt(numOfRecord,10);j++){
records.push({'c':generateText()});
}
saveListObjectStoreValue(db, 'testObject', records);
}
function saveListObjectStoreValue(thisDB, objectStoreName, list){
var transaction = thisDB.transaction(objectStoreName,"readwrite");
var objectStore = transaction.objectStore(objectStoreName);
for( index in list){
var req =objectStore.add(list[index]);
req.onerror = function(event){
alert('error');
}
req.onsuccess = function(event){
}
}
}
function loadDB(){
var request =indexedDB.open("testSafari5",1);
request.onupgradeneeded = function (evt) {
var thisDb = evt.target.result;
if(!thisDb.objectStoreNames.contains("testObject")) {
var objectStore = thisDb.createObjectStore("testObject", { keyPath: "id", autoIncrement:true });
}
};
request.onsuccess = function (evt) {
db = evt.target.result;
document.getElementById('runButton').style.display="";
document.getElementById('resultButton').style.display="";
};
request.onerror = function (evt) {
alert('error');
};
}
function getCount(){
var transaction = db.transaction('testObject',"readwrite");
var objectStore = transaction.objectStore('testObject');
var request = objectStore.openCursor();
var count = 0;
request.onsuccess = function(event) {
var cursor = event.target.result;
if (cursor){
count++;
cursor.continue();
}else {
alert('total number records saved: '+count);
}
};
}
window.addEventListener("DOMContentLoaded", loadDB, false);
function resetValue(){
txt="";
}
</script>
Number of records:
<input type="text" id="numOfRecord" />
Size per record:
<input type="text" id="sizeOfRecords" onchange="resetValue()"/>
kilobytes
<input type="button" value="run test" onclick="executeTest()"
style="display: none" id="runButton" />
<input type="button" value="result" onclick="getCount()"
style="display: none" id="resultButton" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment