Skip to content

Instantly share code, notes, and snippets.

@sorenlouv
Last active December 19, 2023 06:00
Show Gist options
  • Star 53 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save sorenlouv/5083d73f184acae0c5b7 to your computer and use it in GitHub Desktop.
Save sorenlouv/5083d73f184acae0c5b7 to your computer and use it in GitHub Desktop.
A CPU intensive operation. Use to test imitate blocking code, test WebWorkers etc.
function mySlowFunction(baseNumber) {
console.time('mySlowFunction');
let result = 0;
for (var i = Math.pow(baseNumber, 7); i >= 0; i--) {
result += Math.atan(i) * Math.tan(i);
};
console.timeEnd('mySlowFunction');
}
mySlowFunction(8); // higher number => more iterations => slower
@webdevelopland
Copy link

Cool

@jecsham
Copy link

jecsham commented Apr 21, 2022

Nice

@SidduSomething
Copy link

👍

@FPG-Alan
Copy link

Another function with similar functionality seen elsewhere:

function mySlowFunction(blockTime: number) {
	console.time('mySlowFunction');
        const now = performance.now();
	while(performance.now() - now < blockTime) {}
	console.timeEnd('mySlowFunction');
}

mySlowFunction(1000); //millisecond

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment