getAllItems.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf8" />
  5. <title>Simple localForage-getItems (all) 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 = 1000;
  17. function initDb() {
  18. var initPromise = localforage.setDriver(driverTestOrder).then(function() {
  19. return localforage.clear();
  20. }).then(function() {
  21. var totalKeyValues = [];
  22. for (var i = 0; i < n; i++) {
  23. var key = i + 'key';
  24. var value = ((Math.random() * n) | 0) + 'value';
  25. totalKeyValues.push([key, value]);
  26. }
  27. var totalKeyValuesPromises = totalKeyValues.map(function(x) {
  28. return localforage.setItem(x[0], x[1]);
  29. });
  30. var promiseAll = Promise.all(totalKeyValuesPromises);
  31. return promiseAll;
  32. });
  33. return initPromise;
  34. }
  35. Promise.resolve().then(function() {
  36. return time('initDb', function () {
  37. return initDb().then(function() {
  38. console.log(localforage.driver());
  39. });
  40. });
  41. }).then(function(){
  42. return time('getItems()', function () {
  43. return localforage.getItems();
  44. });
  45. }).then(function(){
  46. return time('iterate', function () {
  47. var accumulator = {};
  48. return localforage.iterate(function(value, key/*, iterationNumber*/) {
  49. accumulator[key] = value;
  50. }).then(function() {
  51. return accumulator;
  52. });
  53. });
  54. }).then(function(){
  55. return time('keys + getItems', function () {
  56. return localforage.keys().then(function(keys){
  57. return localforage.getItems(keys);
  58. });
  59. });
  60. }).then(function(){
  61. return time('keys + getItem + Promise.all', function () {
  62. return localforage.keys().then(function(keys){
  63. // return Promise.all(keys.map(function(key){
  64. // return localforage.getItem(key);
  65. // }));
  66. var itemPromises = [];
  67. for (var i = 0, len = keys.length; i < len; i++) {
  68. itemPromises.push(localforage.getItem(keys[i]));
  69. }
  70. return Promise.all(itemPromises);
  71. });
  72. });
  73. });
  74. function time(name, fn) {
  75. var t0 = performance.now();
  76. console.log('Starting ' + name);
  77. var result = fn();
  78. if (typeof result.then === 'function') {
  79. return result.then(logResult);
  80. }
  81. logResult(result);
  82. function logResult(result) {
  83. var t1 = performance.now();
  84. if (result){
  85. console.log(name + ' results', result);
  86. }
  87. console.log('Completed ' + name + ' after ' + (t1 - t0) + ' milliseconds.');
  88. }
  89. return Promise.resolve();
  90. }
  91. </script>
  92. <p>
  93. Check your console log.
  94. </p>
  95. </body>
  96. </html>