Skip to content

Instantly share code, notes, and snippets.

Created August 21, 2013 12:41
Show Gist options
  • Save anonymous/6293955 to your computer and use it in GitHub Desktop.
Save anonymous/6293955 to your computer and use it in GitHub Desktop.
Testing IndexedDB + moment.js performance.
<html>
<head>
<script src="moment.min.js"></script>
<script>
// window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
// window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction;
// window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange;
function getData() {
var data = [],
// random team names list
teamNames = [
'team a',
'team b',
'team c',
'team d'
],
// going to generate 30 days of data starting on this date
startDate = '2013-01-01';
for (day=0;day<100;day++) { // loop through 100 days
date = moment(startDate).add('days',day).toDate();
for (i=0; i< 1000; i++) { // generate 1000 data sets for each day
randomNumber = Math.floor(Math.random() * 100) + 50;
data.push({
eligible: randomNumber,
resolved: (randomNumber - (Math.floor(Math.random() * 20) + 1)),
team: teamNames[Math.floor(Math.random() * 3)],
date: date
});
}
}
console.log(data);
return data;
}
var dailyFCR = {};
function opendb(done) {
var v = 7, request = window.indexedDB.open("datadb",v);
request.onerror = function(e) {
console.dir(e);
debugger;
alert('opendb failed');
};
request.onupgradeneeded = function(e) {
var db = e.target.result;
if (db.objectStoreNames.contains("data")) db.deleteObjectStore("data");
var objectStore = db.createObjectStore("data", { keyPath: 'index' });
var data = getData();
for (var i = 0; i < data.length; i++) {
data[i].index = i;
objectStore.put(data[i]);
}
}
request.onsuccess = function(e) {
var db = e.target.result;
if (v != db.version && db.setVersion) {
var sv = db.setVersion(v);
sv.onsuccess = function() {
request.onupgradeneeded({ target: { result: db }});
db.close();
opendb(done);
}
} else {
done(db);
}
};
}
function test_many_moments(db,c,done) {
dailyFCR = {};
var n = "run " + c + " test: many moment().unix() calls";
console.time(n);
var r = db.transaction("data").objectStore("data").openCursor();
r.onsuccess = function(e) {
result = e.target.result
if (result) {
if (!dailyFCR[moment(result.value.date).unix()]) dailyFCR[moment(result.value.date).unix()] = { eligible: 0, resolved: 0 };
dailyFCR[moment(result.value.date).unix()].eligible += result.value.eligible;
dailyFCR[moment(result.value.date).unix()].resolved += result.value.resolved;
result.continue();
} else {
console.timeEnd(n);
// console.dir(dailyFCR);
if (done) window.setTimeout(done,1000);
}
}
};
function test_single_moment(db,c,done) {
dailyFCR = {};
var n = "run " + c + " test: single moment().unix() call";
console.time(n);
var r = db.transaction("data").objectStore("data").openCursor();
r.onsuccess = function(e) {
result = e.target.result
if (result) {
var m = moment(result.value.date).unix(),
d = dailyFCR[m] || { eligible: 0, resolved: 0 };
d.eligible += result.value.eligible;
d.resolved += result.value.resolved;
dailyFCR[m] = d;
result.continue();
} else {
console.timeEnd(n);
// console.dir(dailyFCR);
if (done) window.setTimeout(done,1000);
}
}
};
function test_valueOf(db,c,done) {
dailyFCR = {};
var n = "run " + c + " test: date.valueOf()";
console.time(n);
var r = db.transaction("data").objectStore("data").openCursor();
r.onsuccess = function(e) {
result = e.target.result
if (result) {
var m = result.value.date.valueOf(),
d = dailyFCR[m] || { eligible: 0, resolved: 0 };
d.eligible += result.value.eligible;
d.resolved += result.value.resolved;
dailyFCR[m] = d;
result.continue();
} else {
console.timeEnd(n);
// console.dir(dailyFCR);
if (done) window.setTimeout(done,1000);
}
}
};
function test_noop(db,c,done) {
dailyFCR = {};
var n = "run " + c + " test: noop";
console.time(n);
var r = db.transaction("data").objectStore("data").openCursor();
r.onsuccess = function(e) {
result = e.target.result
if (result) {
result.continue();
} else {
console.timeEnd(n);
if (done) window.setTimeout(done,1000);
}
}
};
opendb(function(db) {
var count = 0,
runtests = function(done) {
console.log('test run ' + count);
var tests = [ test_noop, test_many_moments, test_single_moment, test_valueOf ],
run = function() {
if (tests.length) {
tests.shift()(db, count, run);
} else {
done();
}
};
run();
},
repeat = function() {
if (++count < 10) runtests(repeat);
};
runtests(repeat);
});
</script>
</head>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment