forked from dexie/Dexie.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests-performance.js
125 lines (106 loc) · 4.7 KB
/
tests-performance.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
///<reference path="run-unit-tests.html" />
(function () {
module("performance", {
setup: function () {
},
teardown: function () {
stop(); Dexie.delete("PerformanceDB").onblocked(function() {
//alert("Please close other browsers and tabs! Another browser or tab is blocking the database from being deleted. ");
}).catch(function (e) {
ok(false, e);
}).finally(function () {
start();
});
}
});
asyncTest("performance: add/equalsIgnoreCase/each", function () {
var db = new Dexie("PerformanceDB");
db.version(1).stores({ emails: "++id,from,to,subject,message,shortStr" });
db.on("blocked", function() {
alert("Please close other browsers and tabs! Another browser or tab is blocking the database from being upgraded or deleted.");
});
var tick;
db.delete().then(function () {
function randomString(count) {
return function () {
var ms = [];
for (var i = 0; i < count; ++i) {
ms.push(String.fromCharCode(32 + Math.floor(Math.random() * 96)));
}
return ms.join('');
}
}
db.open();
tick = Date.now();
// Create 10,000 emails
ok(true, "Creating 10,000 emails");
return db.transaction("rw", db.emails, function() {
for (var i = 1; i <= 10000; ++i) {
db.emails.add({
from: "from" + i + "@test.com",
to: "to" + i + "@test.com",
subject: "subject" + i,
message: "message" + i,
shortStr: randomString(2)()
});
}
});
}).then(function () {
ok(true, "Time taken: " + (Date.now() - tick));
// Speed of equals()
ok(true, "Speed of equals()");
tick = Date.now();
return db.emails.where("shortStr").equals("yk").toArray();
}).then(function (a) {
var tock = Date.now();
ok(true, "Time taken: " + (tock - tick));
ok(true, "Num emails found: " + a.length);
ok(true, "Time taken per found item: " + (tock - tick) / a.length);
// Speed of equalsIgnoreCase()
ok(true, "Speed of equalsIgnoreCase()");
tick = Date.now();
return db.emails.where("shortStr").equalsIgnoreCase("yk").toArray();
}).then (function (a) {
var tock = Date.now();
ok(true, "Time taken: " + (tock - tick));
ok(true, "Num emails found: " + a.length);
ok(true, "Time taken per found item: " + (tock - tick) / a.length);
// Speed of manual filter case insensitive search
ok(true, "Speed of manual filter case insensitive search");
tick = Date.now();
var foundEmails = [];
return db.emails.each(function (email) {
if (email.shortStr.toLowerCase() === "yk")
foundEmails.push(email);
}).then(function () { return foundEmails; });
}).then (function(foundEmails) {
var tock = Date.now();
ok(true, "Time taken: " + (tock - tick));
ok(true, "Num emails found: " + foundEmails.length);
ok(true, "Time taken per found item: " + (tock - tick) / foundEmails.length);
// Measure the time it takes for db.emails.toArra():
ok(true, "Speed of db.emails.toArray()");
tick = Date.now();
return db.emails.toArray();
}).then(function(result) {
var tock = Date.now();
ok(true, "Time taken: " + (tock - tick));
ok(true, "Num emails found: " + result.length);
// Measure the time it takes for db.emails.where('message').startsWith('message').toArray()
ok(true, "Speed of db.emails.where('message').startsWith('message').toArray();");
tick = Date.now();
return db.emails.where('message').startsWith('message').toArray();
}).then(function (result) {
var tock = Date.now();
ok(true, "Time taken: " + (tock - tick));
ok(true, "Num emails found: " + result.length);
}).catch(Error, function (e) {
ok(false, e.name + ":" + e.message + " " + e.stack);
}).catch(function (e) {
ok(false, e.toString());
}).finally(function() {
db.close();
start();
});
});
})();