Skip to content

Instantly share code, notes, and snippets.

@mnieper
Last active March 14, 2017 12:20
Show Gist options
  • Save mnieper/c054ed23296e232336f594f7c5477bac to your computer and use it in GitHub Desktop.
Save mnieper/c054ed23296e232336f594f7c5477bac to your computer and use it in GitHub Desktop.
Drawing outside of viewport (http://jsbench.github.io/#c054ed23296e232336f594f7c5477bac) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Drawing outside of viewport</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2>
</body>
</html>
"use strict";
(function (factory) {
if (typeof Benchmark !== "undefined") {
factory(Benchmark);
} else {
factory(require("benchmark"));
}
})(function (Benchmark) {
var suite = new Benchmark.Suite;
Benchmark.prototype.setup = function () {
var can2 = document.createElement('canvas');
can2.width = 10000;
can2.height = 10000;
};
suite.add("var ctx2 = can2.getContext('2d');", function () {
var ctx2 = can2.getContext('2d');
ctx2.scale(100, 100);
for (var i = 0; i < 10; ++i) {
ctx2.beginPath();
ctx2.strokeStyle = 'red';
ctx2.lineWidth = 4;
ctx2.moveTo(10,10);
ctx2.lineTo(10,30);
ctx2.lineTo(30,30);
ctx2.lineTo(40,70);
ctx2.quadraticCurveTo(72,43,22,12);
ctx2.quadraticCurveTo(12,43,12,102);
ctx2.stroke();
}
});
suite.add("var ctx2 = can2.getContext('2d');", function () {
var ctx2 = can2.getContext('2d');
ctx2.scale(100, 100);
ctx2.translate(200, 200);
for (var i = 0; i < 10; ++i) {
ctx2.beginPath();
ctx2.strokeStyle = 'red';
ctx2.lineWidth = 4;
ctx2.moveTo(10,10);
ctx2.lineTo(10,30);
ctx2.lineTo(30,30);
ctx2.lineTo(40,70);
ctx2.quadraticCurveTo(72,43,22,12);
ctx2.quadraticCurveTo(12,43,12,102);
ctx2.stroke();
}
});
suite.on("cycle", function (evt) {
console.log(" - " + evt.target);
});
suite.on("complete", function (evt) {
console.log(new Array(30).join("-"));
var results = evt.currentTarget.sort(function (a, b) {
return b.hz - a.hz;
});
results.forEach(function (item) {
console.log((idx + 1) + ". " + item);
});
});
console.log("Drawing outside of viewport");
console.log(new Array(30).join("-"));
suite.run();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment