forked from localForage/localForage-getItems
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex2.html
86 lines (73 loc) · 2.48 KB
/
index2.html
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
<!doctype html>
<html>
<head>
<meta charset="utf8" />
<title>Simple localForage-getItems example</title>
</head>
<body>
<script src="../node_modules/localforage/dist/localforage.js"></script>
<script src="../dist/localforage-getitems.js"></script>
<script>
var driverTestOrder = [
localforage.INDEXEDDB,
localforage.WEBSQL,
localforage.LOCALSTORAGE
];
var n = 100;
var take = 10;
function getRandomKeys() {
var keys = [];
for (var i = 0; i < take; i++) {
var key = ((Math.random() * n) | 0) + 'key';
keys.push(key);
}
return keys;
}
var itemKeys = getRandomKeys();
function initDb() {
var initPromise = localforage.setDriver(driverTestOrder).then(function() {
return localforage.clear();
}).then(function() {
var totalKeyValues = [];
for (var i = 0; i < n; i++) {
var key = i + 'key';
var value = ((Math.random() * n) | 0) + 'value';
totalKeyValues.push([key, value]);
}
var totalKeyValuesPromises = totalKeyValues.map(function(x) {
return localforage.setItem(x[0], x[1]);
});
var promiseAll = Promise.all(totalKeyValuesPromises);
return promiseAll;
});
return initPromise;
}
var initT0 = performance.now();
initDb().then(function(){
var initT1 = performance.now();
console.log('Init Finished after ' + (initT1 - initT0) + ' milliseconds.');
var driver = localforage.driver();
Promise.resolve().then(function(){
var t0 = performance.now();
return localforage.getItems(itemKeys).then(function(results){
console.log('getItems', results);
var t1 = performance.now();
console.log('Completed ' + driver + ' after ' + (t1 - t0) + ' milliseconds.');
});
}).then(function(){
if (localforage.driver() !== localforage.LOCALSTORAGE) {
var t0g = performance.now();
localforage.getItems.generic.call(localforage, itemKeys).then(function(results){
console.log('getItemsGeneric', results);
var t1g = performance.now();
console.log('Completed Generic after ' + (t1g - t0g) + ' milliseconds.');
});
}
});
});
</script>
<p>
Check your console log.
</p>
</body>
</html>