├── .gitattributes ├── .gitignore ├── doWork.js ├── webworker.js ├── index.html ├── README.md └── app.js /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear on external disk 35 | .Spotlight-V100 36 | .Trashes 37 | 38 | # Directories potentially created on remote AFP share 39 | .AppleDB 40 | .AppleDesktop 41 | Network Trash Folder 42 | Temporary Items 43 | .apdisk 44 | -------------------------------------------------------------------------------- /doWork.js: -------------------------------------------------------------------------------- 1 | var RaananW; 2 | (function (RaananW) { 3 | var WorkedIDBTest; 4 | (function (WorkedIDBTest) { 5 | function doWork(data, onProgress, onSuccess) { 6 | function sleep(milliseconds) { 7 | var start = new Date().getTime(); 8 | for (var i = 0; i < 1e7; i++) { 9 | if ((new Date().getTime() - start) > milliseconds) { 10 | break; 11 | } 12 | } 13 | } 14 | var lastP = 0; 15 | data.forEach(function (d, idx) { 16 | sleep(d); 17 | var newP = ((idx / data.length) * 100) >> 0; 18 | if (newP > lastP) { 19 | lastP = newP; 20 | onProgress(lastP, d); 21 | } 22 | }); 23 | onSuccess(); 24 | } 25 | WorkedIDBTest.doWork = doWork; 26 | })(WorkedIDBTest = RaananW.WorkedIDBTest || (RaananW.WorkedIDBTest = {})); 27 | })(RaananW || (RaananW = {})); 28 | //# sourceMappingURL=doWork.js.map -------------------------------------------------------------------------------- /webworker.js: -------------------------------------------------------------------------------- 1 | importScripts("doWork.js"); 2 | onmessage = function (e) { 3 | var msg = e.data; 4 | var db; 5 | var request = indexedDB.open(msg.dbName, msg.dbVersion); 6 | request.onerror = function (event) { 7 | alert("Why didn't you allow my test app to use IndexedDB?!"); 8 | }; 9 | request.onsuccess = function (event) { 10 | db = event.target['result']; 11 | var trans = db.transaction("numbers", IDBTransaction.READ_ONLY); 12 | var store = trans.objectStore("numbers"); 13 | var items = []; 14 | trans.oncomplete = function (evt) { 15 | RaananW.WorkedIDBTest.doWork(items, function (progress) { 16 | postMessage(progress); 17 | }, function () { 18 | postMessage("finished"); 19 | }); 20 | }; 21 | var cursorRequest = store.openCursor(); 22 | cursorRequest.onerror = function (error) { 23 | console.log(error); 24 | }; 25 | cursorRequest.onsuccess = function (evt) { 26 | var cursor = evt.target['result']; 27 | if (cursor) { 28 | items.push(cursor.value); 29 | cursor.continue(); 30 | } 31 | }; 32 | }; 33 | }; 34 | //# sourceMappingURL=webworker.js.map 35 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 | 6 |53 | This demo shows one of the benefits of using indexed db in a webworker. 54 |
55 |56 | The generated data, which is an array of integers is used to simulate "work". 57 | The data is being sent to the WebWorker using an IndexedDB database, which is being populated on the main thread. The WebWorker then fetches the entire data set and processes it. 58 |
59 |60 | Processing the data in the main thread will caused the browser to be "stuck" for a while, while the webworker doesn't halt the animation and actually updates the UI correctly. 61 |
62 |63 | The lower progress bar will turn green after the data was completely processed. 64 |
65 |66 | More @ GitHub. 67 |
68 |70 | To generate a new set of data (delays) choose the properties and press "Generate data". 71 |
72 |73 | To see how this data is being processed on the main thread or persisted in IndexedDB and processed in a webworker click the corresponding buttons. 74 |
75 |76 | Try 10000 : 1 , 2500 : 10, 1000 : 25 . You will notice the overhead of storing the data and reading it entirely is not affecting the site's performance. 77 |
78 |