Skip to content

Instantly share code, notes, and snippets.

@dghez
Last active January 18, 2022 14:25
Show Gist options
  • Save dghez/f2f3a5ac84d58d807efa51edf2ff07c9 to your computer and use it in GitHub Desktop.
Save dghez/f2f3a5ac84d58d807efa51edf2ff07c9 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
checkPerf() {
// performance test based on cpu performance
// higher score means worse performance
// based on Baptiste Briel's work: http://awams.bbriel.me/27
// Copied from: https://gist.github.com/martinlaxenaire/27cf27f043ec5ce423ce165e283fc19a
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
let 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