Skip to content

Instantly share code, notes, and snippets.

@lifeart
Last active September 6, 2017 11:49
Show Gist options
  • Save lifeart/716d7f0bb087c34247487e4abee2419c to your computer and use it in GitHub Desktop.
Save lifeart/716d7f0bb087c34247487e4abee2419c to your computer and use it in GitHub Desktop.
WebWorker JSON Parse - async json parsing
VM818:24 Starting...
VM818:31 Buiding test Data: 6429.308837890625ms
VM818:35 1498727454505
VM818:37 Bloking event loop...
VM818:39 StandartParsing: 2425.947021484375ms
VM818:40 18000000
VM818:41 Event loop unblocked
VM818:33 1498727457005 - event loop not blocked
VM818:33 1498727457015 - event loop not blocked
VM818:33 1498727457025 - event loop not blocked
VM818:33 1498727457035 - event loop not blocked
VM818:33 1498727457046 - event loop not blocked
VM818:33 1498727457056 - event loop not blocked
VM818:33 1498727457065 - event loop not blocked
VM818:33 1498727457075 - event loop not blocked
VM818:33 1498727457085 - event loop not blocked
VM818:33 1498727457095 - event loop not blocked
VM818:33 1498727457106 - event loop not blocked
VM818:33 1498727457116 - event loop not blocked
VM818:33 1498727457125 - event loop not blocked
VM818:33 1498727457136 - event loop not blocked
VM818:33 1498727457145 - event loop not blocked
VM818:33 1498727457155 - event loop not blocked
VM818:33 1498727457166 - event loop not blocked
VM818:33 1498727457175 - event loop not blocked
VM818:33 1498727457185 - event loop not blocked
VM818:33 1498727457195 - event loop not blocked
VM818:33 1498727457206 - event loop not blocked
VM818:33 1498727457215 - event loop not blocked
VM818:33 1498727457225 - event loop not blocked
VM818:33 1498727457236 - event loop not blocked
VM818:33 1498727457246 - event loop not blocked
VM818:33 1498727457255 - event loop not blocked
VM818:33 1498727457266 - event loop not blocked
VM818:33 1498727457275 - event loop not blocked
VM818:33 1498727457285 - event loop not blocked
VM818:33 1498727457296 - event loop not blocked
VM818:33 1498727457305 - event loop not blocked
VM818:33 1498727457315 - event loop not blocked
VM818:33 1498727457326 - event loop not blocked
VM818:33 1498727457336 - event loop not blocked
VM818:33 1498727457346 - event loop not blocked
VM818:33 1498727457356 - event loop not blocked
VM818:33 1498727457366 - event loop not blocked
VM818:33 1498727457377 - event loop not blocked
VM818:33 1498727457386 - event loop not blocked
VM818:33 1498727457397 - event loop not blocked
VM818:33 1498727457406 - event loop not blocked
VM818:33 1498727457416 - event loop not blocked
VM818:33 1498727457426 - event loop not blocked
VM818:33 1498727457437 - event loop not blocked
VM818:33 1498727457446 - event loop not blocked
VM818:33 1498727457461 - event loop not blocked
VM818:33 1498727457467 - event loop not blocked
VM818:33 1498727457476 - event loop not blocked
VM818:33 1498727457486 - event loop not blocked
VM818:33 1498727457496 - event loop not blocked
VM818:33 1498727457507 - event loop not blocked
VM818:33 1498727457516 - event loop not blocked
VM818:33 1498727457527 - event loop not blocked
VM818:33 1498727457536 - event loop not blocked
VM818:33 1498727457546 - event loop not blocked
VM818:33 1498727457559 - event loop not blocked
VM818:33 1498727457566 - event loop not blocked
VM818:33 1498727457576 - event loop not blocked
VM818:33 1498727457586 - event loop not blocked
VM818:33 1498727457596 - event loop not blocked
VM818:33 1498727457607 - event loop not blocked
VM818:33 1498727457617 - event loop not blocked
VM818:33 1498727457627 - event loop not blocked
VM818:33 1498727457637 - event loop not blocked
VM818:33 1498727457648 - event loop not blocked
VM818:33 1498727457658 - event loop not blocked
VM818:33 1498727457668 - event loop not blocked
VM818:33 1498727457676 - event loop not blocked
VM818:33 1498727457687 - event loop not blocked
VM818:33 1498727457696 - event loop not blocked
VM818:33 1498727457706 - event loop not blocked
VM818:33 1498727457716 - event loop not blocked
VM818:33 1498727457727 - event loop not blocked
VM818:33 1498727457736 - event loop not blocked
VM818:33 1498727457746 - event loop not blocked
VM818:33 1498727457762 - event loop not blocked
VM818:33 1498727457766 - event loop not blocked
VM818:33 1498727457776 - event loop not blocked
VM818:33 1498727457787 - event loop not blocked
VM818:33 1498727457797 - event loop not blocked
VM818:33 1498727457806 - event loop not blocked
VM818:33 1498727457816 - event loop not blocked
VM818:33 1498727457826 - event loop not blocked
VM818:33 1498727457837 - event loop not blocked
VM818:33 1498727457864 - event loop not blocked
VM818:33 1498727457869 - event loop not blocked
VM818:33 1498727457892 - event loop not blocked
VM818:33 1498727457896 - event loop not blocked
VM818:33 1498727457907 - event loop not blocked
VM818:33 1498727457916 - event loop not blocked
VM818:33 1498727457926 - event loop not blocked
VM818:33 1498727457936 - event loop not blocked
VM818:33 1498727457945 - event loop not blocked
VM818:33 1498727457957 - event loop not blocked
VM818:33 1498727457966 - event loop not blocked
VM818:33 1498727457976 - event loop not blocked
VM818:33 1498727457990 - event loop not blocked
VM818:33 1498727457996 - event loop not blocked
VM818:33 1498727458007 - event loop not blocked
VM818:33 1498727458017 - event loop not blocked
VM818:33 1498727458027 - event loop not blocked
VM818:33 1498727458036 - event loop not blocked
VM818:33 1498727458046 - event loop not blocked
VM818:33 1498727458056 - event loop not blocked
VM818:33 1498727458066 - event loop not blocked
VM818:33 1498727458076 - event loop not blocked
VM818:33 1498727458087 - event loop not blocked
VM818:33 1498727458096 - event loop not blocked
VM818:33 1498727458106 - event loop not blocked
VM818:33 1498727458116 - event loop not blocked
VM818:33 1498727458126 - event loop not blocked
VM818:33 1498727458136 - event loop not blocked
VM818:33 1498727458147 - event loop not blocked
VM818:33 1498727458156 - event loop not blocked
VM818:33 1498727458166 - event loop not blocked
VM818:33 1498727458176 - event loop not blocked
VM818:33 1498727458187 - event loop not blocked
VM818:33 1498727458196 - event loop not blocked
VM818:33 1498727458206 - event loop not blocked
VM818:33 1498727458217 - event loop not blocked
VM818:33 1498727458227 - event loop not blocked
VM818:33 1498727458236 - event loop not blocked
VM818:33 1498727458247 - event loop not blocked
VM818:33 1498727458256 - event loop not blocked
VM818:33 1498727458266 - event loop not blocked
VM818:33 1498727458276 - event loop not blocked
VM818:33 1498727458287 - event loop not blocked
VM818:33 1498727458296 - event loop not blocked
VM818:33 1498727458306 - event loop not blocked
VM818:33 1498727458316 - event loop not blocked
VM818:33 1498727458326 - event loop not blocked
VM818:33 1498727458335 - event loop not blocked
VM818:33 1498727458346 - event loop not blocked
VM818:33 1498727458356 - event loop not blocked
VM818:33 1498727458366 - event loop not blocked
VM818:33 1498727458376 - event loop not blocked
VM818:33 1498727458386 - event loop not blocked
VM818:33 1498727458396 - event loop not blocked
VM818:33 1498727458406 - event loop not blocked
VM818:33 1498727458417 - event loop not blocked
VM818:33 1498727458426 - event loop not blocked
VM818:33 1498727458436 - event loop not blocked
VM818:33 1498727458446 - event loop not blocked
VM818:33 1498727458456 - event loop not blocked
VM818:33 1498727458466 - event loop not blocked
VM818:33 1498727458476 - event loop not blocked
VM818:33 1498727458486 - event loop not blocked
VM818:33 1498727458496 - event loop not blocked
VM818:33 1498727458507 - event loop not blocked
VM818:33 1498727458516 - event loop not blocked
VM818:33 1498727458526 - event loop not blocked
VM818:33 1498727458535 - event loop not blocked
VM818:33 1498727458547 - event loop not blocked
VM818:33 1498727458556 - event loop not blocked
VM818:33 1498727458566 - event loop not blocked
VM818:33 1498727458576 - event loop not blocked
VM818:33 1498727458587 - event loop not blocked
VM818:33 1498727458596 - event loop not blocked
VM818:33 1498727458606 - event loop not blocked
VM818:33 1498727458616 - event loop not blocked
VM818:33 1498727458627 - event loop not blocked
VM818:33 1498727458635 - event loop not blocked
VM818:33 1498727458647 - event loop not blocked
VM818:33 1498727458657 - event loop not blocked
VM818:33 1498727458666 - event loop not blocked
VM818:33 1498727458676 - event loop not blocked
VM818:33 1498727458689 - event loop not blocked
VM818:33 1498727458696 - event loop not blocked
VM818:33 1498727458706 - event loop not blocked
VM818:33 1498727458716 - event loop not blocked
VM818:33 1498727458726 - event loop not blocked
VM818:33 1498727458735 - event loop not blocked
VM818:33 1498727458746 - event loop not blocked
VM818:33 1498727458757 - event loop not blocked
VM818:33 1498727458766 - event loop not blocked
VM818:33 1498727458779 - event loop not blocked
VM818:33 1498727458786 - event loop not blocked
VM818:33 1498727458796 - event loop not blocked
VM818:33 1498727458807 - event loop not blocked
VM818:33 1498727458817 - event loop not blocked
VM818:33 1498727458826 - event loop not blocked
VM818:33 1498727458836 - event loop not blocked
VM818:33 1498727458847 - event loop not blocked
VM818:33 1498727458855 - event loop not blocked
VM818:33 1498727458879 - event loop not blocked
VM818:33 1498727458900 - event loop not blocked
VM818:33 1498727458907 - event loop not blocked
VM818:33 1498727458916 - event loop not blocked
VM818:33 1498727458926 - event loop not blocked
VM818:33 1498727458935 - event loop not blocked
VM818:33 1498727458947 - event loop not blocked
VM818:33 1498727458956 - event loop not blocked
VM818:33 1498727458966 - event loop not blocked
VM818:33 1498727458979 - event loop not blocked
VM818:33 1498727458989 - event loop not blocked
VM818:33 1498727458997 - event loop not blocked
VM818:33 1498727459007 - event loop not blocked
VM818:33 1498727459017 - event loop not blocked
VM818:33 1498727459026 - event loop not blocked
VM818:33 1498727459035 - event loop not blocked
VM818:33 1498727459047 - event loop not blocked
VM818:33 1498727459057 - event loop not blocked
VM818:33 1498727459066 - event loop not blocked
VM818:33 1498727459076 - event loop not blocked
VM818:33 1498727459087 - event loop not blocked
VM818:33 1498727459096 - event loop not blocked
VM818:33 1498727459106 - event loop not blocked
VM818:33 1498727459116 - event loop not blocked
VM818:33 1498727459126 - event loop not blocked
VM818:33 1498727459135 - event loop not blocked
VM818:33 1498727459146 - event loop not blocked
VM818:33 1498727459156 - event loop not blocked
VM818:33 1498727459166 - event loop not blocked
VM818:33 1498727459176 - event loop not blocked
VM818:33 1498727459186 - event loop not blocked
VM818:33 1498727459196 - event loop not blocked
VM818:33 1498727459206 - event loop not blocked
VM818:33 1498727459217 - event loop not blocked
VM818:33 1498727459226 - event loop not blocked
VM818:33 1498727459235 - event loop not blocked
VM818:33 1498727459247 - event loop not blocked
VM818:33 1498727459257 - event loop not blocked
VM818:33 1498727459267 - event loop not blocked
VM818:33 1498727459276 - event loop not blocked
VM818:33 1498727459287 - event loop not blocked
VM818:33 1498727459296 - event loop not blocked
VM818:33 1498727459306 - event loop not blocked
VM818:33 1498727459317 - event loop not blocked
VM818:33 1498727459326 - event loop not blocked
VM818:33 1498727459336 - event loop not blocked
VM818:33 1498727459347 - event loop not blocked
VM818:33 1498727459356 - event loop not blocked
VM818:33 1498727459366 - event loop not blocked
VM818:33 1498727459377 - event loop not blocked
VM818:33 1498727459386 - event loop not blocked
VM818:33 1498727459396 - event loop not blocked
VM818:33 1498727459405 - event loop not blocked
VM818:33 1498727459416 - event loop not blocked
VM818:33 1498727459426 - event loop not blocked
VM818:33 1498727459437 - event loop not blocked
VM818:33 1498727459446 - event loop not blocked
VM818:33 1498727459456 - event loop not blocked
VM818:33 1498727459466 - event loop not blocked
VM818:33 1498727459476 - event loop not blocked
VM818:33 1498727459486 - event loop not blocked
VM818:33 1498727459496 - event loop not blocked
VM818:33 1498727459506 - event loop not blocked
VM818:33 1498727459516 - event loop not blocked
VM818:33 1498727459526 - event loop not blocked
VM818:33 1498727459536 - event loop not blocked
VM818:33 1498727459547 - event loop not blocked
VM818:33 1498727459556 - event loop not blocked
VM818:33 1498727459567 - event loop not blocked
VM818:33 1498727459577 - event loop not blocked
VM818:33 1498727459586 - event loop not blocked
VM818:33 1498727459597 - event loop not blocked
VM818:33 1498727459606 - event loop not blocked
VM818:33 1498727459617 - event loop not blocked
VM818:33 1498727459627 - event loop not blocked
VM818:33 1498727459636 - event loop not blocked
VM818:33 1498727459647 - event loop not blocked
VM818:33 1498727459656 - event loop not blocked
VM818:33 1498727459667 - event loop not blocked
VM818:33 1498727459678 - event loop not blocked
VM818:33 1498727459687 - event loop not blocked
VM818:33 1498727459696 - event loop not blocked
VM818:33 1498727459707 - event loop not blocked
VM818:33 1498727459717 - event loop not blocked
VM818:33 1498727459726 - event loop not blocked
VM818:33 1498727459736 - event loop not blocked
VM818:33 1498727459747 - event loop not blocked
VM818:33 1498727459757 - event loop not blocked
VM818:33 1498727459766 - event loop not blocked
VM818:33 1498727459776 - event loop not blocked
VM818:33 1498727459786 - event loop not blocked
VM818:33 1498727459796 - event loop not blocked
VM818:33 1498727459807 - event loop not blocked
VM818:33 1498727459816 - event loop not blocked
VM818:33 1498727459827 - event loop not blocked
VM818:33 1498727459836 - event loop not blocked
VM818:33 1498727459846 - event loop not blocked
VM818:33 1498727459856 - event loop not blocked
VM818:33 1498727459865 - event loop not blocked
VM818:33 1498727459876 - event loop not blocked
VM818:33 1498727459895 - event loop not blocked
VM818:33 1498727459906 - event loop not blocked
VM818:33 1498727459915 - event loop not blocked
VM818:33 1498727459926 - event loop not blocked
VM818:33 1498727459936 - event loop not blocked
VM818:33 1498727459947 - event loop not blocked
VM818:33 1498727459976 - event loop not blocked
VM818:33 1498727459993 - event loop not blocked
VM818:33 1498727459997 - event loop not blocked
VM818:33 1498727460006 - event loop not blocked
VM818:33 1498727460016 - event loop not blocked
VM818:33 1498727460026 - event loop not blocked
VM818:33 1498727460036 - event loop not blocked
VM818:33 1498727460046 - event loop not blocked
VM818:33 1498727460056 - event loop not blocked
VM818:33 1498727460067 - event loop not blocked
VM818:33 1498727460077 - event loop not blocked
VM818:33 1498727460087 - event loop not blocked
VM818:33 1498727460096 - event loop not blocked
VM818:33 1498727460106 - event loop not blocked
VM818:33 1498727460117 - event loop not blocked
VM818:33 1498727460127 - event loop not blocked
VM818:33 1498727460137 - event loop not blocked
VM818:33 1498727460147 - event loop not blocked
VM818:33 1498727460157 - event loop not blocked
VM818:33 1498727460165 - event loop not blocked
VM818:33 1498727460176 - event loop not blocked
VM818:33 1498727460186 - event loop not blocked
VM818:33 1498727460197 - event loop not blocked
VM818:33 1498727460207 - event loop not blocked
VM818:33 1498727460217 - event loop not blocked
VM818:33 1498727460226 - event loop not blocked
VM818:33 1498727460236 - event loop not blocked
VM818:33 1498727460246 - event loop not blocked
VM818:33 1498727460256 - event loop not blocked
VM818:33 1498727460266 - event loop not blocked
VM818:33 1498727460276 - event loop not blocked
VM818:33 1498727460286 - event loop not blocked
VM818:33 1498727460297 - event loop not blocked
VM818:33 1498727460306 - event loop not blocked
VM818:33 1498727460315 - event loop not blocked
VM818:33 1498727460327 - event loop not blocked
VM818:33 1498727460336 - event loop not blocked
VM818:33 1498727460347 - event loop not blocked
VM818:33 1498727460356 - event loop not blocked
VM818:33 1498727460367 - event loop not blocked
VM818:33 1498727460376 - event loop not blocked
VM818:33 1498727460386 - event loop not blocked
VM818:33 1498727460395 - event loop not blocked
VM818:33 1498727460405 - event loop not blocked
VM818:33 1498727460415 - event loop not blocked
VM818:33 1498727460425 - event loop not blocked
VM818:33 1498727460436 - event loop not blocked
VM818:33 1498727460445 - event loop not blocked
VM818:33 1498727460456 - event loop not blocked
VM818:33 1498727460465 - event loop not blocked
VM818:33 1498727460475 - event loop not blocked
VM818:33 1498727460485 - event loop not blocked
VM818:33 1498727460495 - event loop not blocked
VM818:33 1498727460505 - event loop not blocked
VM818:33 1498727460515 - event loop not blocked
VM818:33 1498727460525 - event loop not blocked
VM818:33 1498727460535 - event loop not blocked
VM818:33 1498727460546 - event loop not blocked
VM818:33 1498727460557 - event loop not blocked
VM818:33 1498727460567 - event loop not blocked
VM818:33 1498727460577 - event loop not blocked
VM818:33 1498727460587 - event loop not blocked
VM818:33 1498727460597 - event loop not blocked
VM818:33 1498727460606 - event loop not blocked
VM818:33 1498727460616 - event loop not blocked
VM818:33 1498727460626 - event loop not blocked
VM818:33 1498727460636 - event loop not blocked
VM818:33 1498727460647 - event loop not blocked
VM818:33 1498727460656 - event loop not blocked
VM818:33 1498727460666 - event loop not blocked
VM818:33 1498727460676 - event loop not blocked
VM818:33 1498727460686 - event loop not blocked
VM818:33 1498727460696 - event loop not blocked
VM818:33 1498727460706 - event loop not blocked
VM818:33 1498727460716 - event loop not blocked
VM818:33 1498727460726 - event loop not blocked
VM818:33 1498727460736 - event loop not blocked
VM818:33 1498727460746 - event loop not blocked
VM818:33 1498727460756 - event loop not blocked
VM818:33 1498727460767 - event loop not blocked
VM818:33 1498727460776 - event loop not blocked
VM818:33 1498727460786 - event loop not blocked
VM818:33 1498727460796 - event loop not blocked
VM818:33 1498727460806 - event loop not blocked
VM818:33 1498727460816 - event loop not blocked
VM818:33 1498727460826 - event loop not blocked
VM818:33 1498727460836 - event loop not blocked
VM818:33 1498727460846 - event loop not blocked
VM818:33 1498727460857 - event loop not blocked
VM818:33 1498727460866 - event loop not blocked
VM818:33 1498727460876 - event loop not blocked
VM818:33 1498727460886 - event loop not blocked
VM818:33 1498727460896 - event loop not blocked
VM818:33 1498727460907 - event loop not blocked
VM818:33 1498727460916 - event loop not blocked
VM818:33 1498727460926 - event loop not blocked
VM818:44 WebWorkerParsing: 5697.458984375ms
VM818:45 18000000
console.log('Starting...');
console.time('Buiding test Data');
const jsonData = {data:new Array(9000000)};
for (let i = 0; i < 9000000; i++) {
jsonData.data.push(`el-${i}`);
}
const dataString = JSON.stringify(jsonData);
console.timeEnd('Buiding test Data');
const interval = setInterval(()=>{
console.log(`${Date.now()} - event loop not blocked`);
},10);
console.log(Date.now());
console.time('StandartParsing');
console.log('Bloking event loop...');
const n = JSON.parse(dataString);
console.timeEnd('StandartParsing');
console.log(n.data.length);
console.log('Event loop unblocked');
console.time('WebWorkerParsing');
webWorkerJSONParse(dataString,(e)=>{
console.timeEnd('WebWorkerParsing');
console.log(e.data.length);
clearInterval(interval);
});
function webWorkerJSONParse( data, callback) {
if (typeof data !== 'string' || data.charAt(0)!=='{') {
callback(data);
return;
}
const WT = `
function () {
self.onmessage = function(e){
self.postMessage(JSON.parse(e.data));
self.close();
};
}
`;
const worker = new Worker(URL.createObjectURL(new Blob(["("+WT+")()"], {type: 'text/javascript'})));
worker.onmessage = (e) => {
callback(e.data);
};
worker.postMessage(data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment