Skip to content

Instantly share code, notes, and snippets.

@FPG-Alan
Forked from sorenlouv/cpu-intensive.js
Created August 31, 2022 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FPG-Alan/2a9d33ff220369e63f0282d893205f31 to your computer and use it in GitHub Desktop.
Save FPG-Alan/2a9d33ff220369e63f0282d893205f31 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
@FPG-Alan
Copy link
Author

FPG-Alan commented Aug 31, 2022

另一个版本

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

mySlowFunction(1000);

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