You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title></head><body>
embedded worker approach example
<button>check if main thread is blocked</button><scripttype="text/javascript">constprocess=f=>{constisOkWorker=Blob&&URL&&URL.createObjectURL;if(isOkWorker){constblob=newBlob([`onmessage = ({data}) => postMessage((${f})(data));`],{type: 'text/javascript'});consturl=URL.createObjectURL(blob);constworker=newWorker(url);constonMessage=(res)=>worker.onmessage=({data})=>res(data);constonError=(rej)=>worker.onerror=({data})=>rej(data);returndata=>newPromise((res,rej)=>{onMessage(res);onError(rej);worker.postMessage(data)})}else{returndata=>f(data);}}constw=process(a=>a+1);constk=w(100).then(res=>console.log('res >>>',res));console.log("🚀 ~ file: index.html:33 ~ k:",k);/** cpu intensive function block main thread 1 sec */functionblockCpuFor1Sec(sec=1){conststartTime=Date.now();// A simple, CPU-intensive, inefficient Fibonacci functionfunctioncalculateFibonacci(n){if(n<2){returnn;}returncalculateFibonacci(n-1)+calculateFibonacci(n-2);}// Find the highest Fibonacci number it can calculate within 1 secondletn=0;while(Date.now()-startTime<sec*1000){calculateFibonacci(n);n++;}console.log(`Calculated Fibonacci(${n}) in 1 second`);}blockCpuFor1Sec(1)</script></body></html>
참조:
https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers
https://www.bsidesoft.com/8167
The text was updated successfully, but these errors were encountered: