Last active
February 7, 2024 10:37
-
-
Save martinlaxenaire/27cf27f043ec5ce423ce165e283fc19a to your computer and use it in GitHub Desktop.
A little function that performs a CPU performance check, based on a similar piece of code orriginally made by Baptise Briel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
checkPerf() { | |
// performance test based on cpu performance | |
// higher score means worse performance | |
// based on Baptiste Briel's work: http://awams.bbriel.me/27 | |
let perf = 0; | |
const start = (performance || Date).now(); | |
let array = []; | |
for(let i = 0; i < 20000; i++) { | |
array = Math.pow(Math.sin(Math.random()), 2); | |
} | |
const perfTest = (performance || Date).now() - start; | |
if(perfTest < 7) { | |
// good | |
perf = 1; | |
} | |
else if(perfTest < 14) { | |
// medium | |
perf = 2; | |
} | |
else if(perfTest < 22) { | |
// low | |
perf = 3; | |
} | |
else { | |
// bad: do not use WebGL | |
perf = 4; | |
} | |
// if reduced-motion is on, ship the lightest version possible | |
const reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce'); | |
if(!reduceMotion || reduceMotion.matches) { | |
perf = 6; | |
} | |
return perf; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment