Skip to content

Instantly share code, notes, and snippets.

@RubaXa
Created November 20, 2015 19:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save RubaXa/a9283bb254143ea63d7c to your computer and use it in GitHub Desktop.
Save RubaXa/a9283bb254143ea63d7c to your computer and use it in GitHub Desktop.
CSS vs. Inline style (http://jsbench.github.io/#a9283bb254143ea63d7c) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>CSS vs. Inline style</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 box = document.createElement('div');
document.body.appendChild(box);
function tpl(fn) {
return Array.from(Array(10)).map(fn);
}
var style = {
a: 'border: 2px solid red;font-size: 20px;',
aa: 'border: 1px dotted green;font-size: 10px;',
aaa: 'border: 3px dashed blue;font-size: 30px;line-height: 10px;'
};
html.innerHTML =
'<style>\
.css {' + style.a + '}\
.css .css {' + style.aa + '}\
.css .css .css {' + style.aaa + '}\
</style>';
};
Benchmark.prototype.teardown = function () {
box.parentNode.removeChild(box);
};
suite.add("CSS", function () {
/* CSS */
box.innerHTML = tpl(function () {
return '<div class="css"><div class="css"><div class="css"></div></div></div>';
}).join('\n');
});
suite.add("Inline", function () {
/* Inline */
box.innerHTML = tpl(function () {
return '<div style="' + style.a + '"><div style="' + style.aa + '"><div style="' + style.aaa + '"></div></div></div>';
});
});
suite.on("cycle", function (evt) {
console.log(" " + evt);
});
suite.on("complete", function (evt) {
var results = evt.currentTarget.sort(function (a, b) {
return b.hz - a.hz;
});
results.forEach(function (item) {
console.log(" " + item);
});
});
console.log("CSS vs. Inline style");
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