Skip to content

Instantly share code, notes, and snippets.

@Neonit
Created May 3, 2017 07:42
Show Gist options
  • Save Neonit/1d9b78eaa605a481f7612df151527ff4 to your computer and use it in GitHub Desktop.
Save Neonit/1d9b78eaa605a481f7612df151527ff4 to your computer and use it in GitHub Desktop.
Manual HTML building vs. innerHTML (https://jsbench.github.io/#1d9b78eaa605a481f7612df151527ff4) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Manual HTML building vs. innerHTML</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;
suite.add("frag = document.createDocumentFragment();", function () {
frag = document.createDocumentFragment();
header = document.createElement('header');
header.className = 'header';
frag.appendChild(header);
heading = document.createElement('h2');
header.appendChild(heading);
icon = document.createElement('i');
icon.className = 'fa fa-banana';
heading.appendChild(icon);
label = document.createTextNode('This is a title');
heading.appendChild(label);
});
suite.add("tmpl = document.createElement('template');", function () {
tmpl = document.createElement('template');
tmpl.innerHTML = '<header class="header"><h2><i class="fa fa-banana"></i> This is a title</h2></header>';
});
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("Manual HTML building vs. innerHTML");
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