index2.html 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf8" />
  5. <title>Simple localForage-getItems example</title>
  6. </head>
  7. <body>
  8. <script src="../node_modules/localforage/dist/localforage.js"></script>
  9. <script src="../dist/localforage-getitems.js"></script>
  10. <script>
  11. var driverTestOrder = [
  12. localforage.INDEXEDDB,
  13. localforage.WEBSQL,
  14. localforage.LOCALSTORAGE
  15. ];
  16. var n = 100;
  17. var take = 10;
  18. function getRandomKeys() {
  19. var keys = [];
  20. for (var i = 0; i < take; i++) {
  21. var key = ((Math.random() * n) | 0) + 'key';
  22. keys.push(key);
  23. }
  24. return keys;
  25. }
  26. var itemKeys = getRandomKeys();
  27. function initDb() {
  28. var initPromise = localforage.setDriver(driverTestOrder).then(function() {
  29. return localforage.clear();
  30. }).then(function() {
  31. var totalKeyValues = [];
  32. for (var i = 0; i < n; i++) {
  33. var key = i + 'key';
  34. var value = ((Math.random() * n) | 0) + 'value';
  35. totalKeyValues.push([key, value]);
  36. }
  37. var totalKeyValuesPromises = totalKeyValues.map(function(x) {
  38. return localforage.setItem(x[0], x[1]);
  39. });
  40. var promiseAll = Promise.all(totalKeyValuesPromises);
  41. return promiseAll;
  42. });
  43. return initPromise;
  44. }
  45. var initT0 = performance.now();
  46. initDb().then(function(){
  47. var initT1 = performance.now();
  48. console.log('Init Finished after ' + (initT1 - initT0) + ' milliseconds.');
  49. var driver = localforage.driver();
  50. Promise.resolve().then(function(){
  51. var t0 = performance.now();
  52. return localforage.getItems(itemKeys).then(function(results){
  53. console.log('getItems', results);
  54. var t1 = performance.now();
  55. console.log('Completed ' + driver + ' after ' + (t1 - t0) + ' milliseconds.');
  56. });
  57. }).then(function(){
  58. if (localforage.driver() !== localforage.LOCALSTORAGE) {
  59. var t0g = performance.now();
  60. localforage.getItems.generic.call(localforage, itemKeys).then(function(results){
  61. console.log('getItemsGeneric', results);
  62. var t1g = performance.now();
  63. console.log('Completed Generic after ' + (t1g - t0g) + ' milliseconds.');
  64. });
  65. }
  66. });
  67. });
  68. </script>
  69. <p>
  70. Check your console log.
  71. </p>
  72. </body>
  73. </html>