index2.html 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf8" />
  5. <title>Simple localForage-setItems example</title>
  6. </head>
  7. <body>
  8. <script src="../node_modules/localforage/dist/localforage.js"></script>
  9. <script src="../dist/localforage-setitems.js"></script>
  10. <script>
  11. var driverTestOrder = [
  12. localforage.INDEXEDDB,
  13. localforage.WEBSQL,
  14. localforage.LOCALSTORAGE
  15. ];
  16. var n = 10000;
  17. function getRandomItems() {
  18. var result = {};
  19. for (var i = 0; i < n; i++) {
  20. var key = ((Math.random() * n) | 0) + 'key';
  21. var value = ((Math.random() * n) | 0) + 'value';
  22. result[key] = value;
  23. }
  24. return result;
  25. }
  26. var items = getRandomItems();
  27. var driver = '';
  28. localforage.setDriver(driverTestOrder).then(function() {
  29. driver = localforage.driver();
  30. return localforage.clear();
  31. }).then(function() {
  32. var t0 = performance.now();
  33. var putDone = localforage.setItems(items);
  34. return putDone.then(function() {
  35. var t1 = performance.now();
  36. console.log('Completed setItems ' + driver + ' after ' + (t1 - t0) + ' milliseconds.');
  37. return localforage.keys().then(function (keys) {
  38. console.log(keys);
  39. return localforage.getItem(keys[0]).then(function (x) {
  40. console.log(keys[0], x);
  41. });
  42. });
  43. });
  44. }).then(function(){
  45. return localforage.clear();
  46. }).then(function(){
  47. if (driver !== localforage.LOCALSTORAGE) {
  48. var t0 = performance.now();
  49. var putDone = localforage.setItems.generic.call(localforage, items);
  50. return putDone.then(function() {
  51. var t1 = performance.now();
  52. console.log('Completed setItemsGeneric ' + driver + ' after ' + (t1 - t0) + ' milliseconds.');
  53. return localforage.keys().then(function (keys) {
  54. console.log(keys);
  55. return localforage.getItem(keys[0]).then(function (x) {
  56. console.log(keys[0], x);
  57. });
  58. });
  59. });
  60. }
  61. });
  62. </script>
  63. <p>
  64. Check your console log.
  65. </p>
  66. </body>
  67. </html>