Skip to content

Instantly share code, notes, and snippets.

@mems
Last active March 29, 2016 17:14
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 mems/39831f33d2a3372af1ff to your computer and use it in GitHub Desktop.
Save mems/39831f33d2a3372af1ff to your computer and use it in GitHub Desktop.
Canvas stress test. Usefull for detect low-end devices or old tablets/smartphones
<!DOCTYPE html PUBLIC>
<html>
<head>
<title></title>
</head>
<body>
<script>
// Polyfill
window.performance = (window.performance || {
offset: Date.now(),
now: function now(){
return Date.now() - this.offset;
}
});
var canvas = document.createElement("CANVAS");
canvas.width = canvas.height = 1000;
var context = canvas.getContext("2d");
var t0 = performance.now();
// first call drawImage twice. Required by Chrome, Opera, IE and Safari Mobile: it take time at cold start, not in next reloads
context.drawImage(canvas, 0, 0, 1, 1);
context.drawImage(canvas, 0, 0, 1, 1);
var t1 = performance.now();
for(var iter = 0; iter < 50; iter++){
context.translate(-10, -10);
context.rotate(Math.PI/2);
context.translate(10, 10);
context.drawImage(canvas, 0, 0, 20, 20, 0, 0, 19, 19);
}
var t2 = performance.now();
document.body.textContent = "\ncanvas stress test: "+(t2 - t1).toFixed(4)+"ms (init: "+(t1 - t0).toFixed(4)+"ms)";
</script>
</body>
</html>
@mems
Copy link
Author

mems commented Mar 29, 2016

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