Skip to content

Instantly share code, notes, and snippets.

@Freak613
Last active April 19, 2019 00:31
Show Gist options
  • Save Freak613/91a09b3ee7a080d7f7aa8ceaceeb1a1d to your computer and use it in GitHub Desktop.
Save Freak613/91a09b3ee7a080d7f7aa8ceaceeb1a1d to your computer and use it in GitHub Desktop.
childNodes indexOf #jsbench #jsperf (https://jsbench.github.io/#91a09b3ee7a080d7f7aa8ceaceeb1a1d) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>childNodes indexOf #jsbench #jsperf</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 () {
if (!window.initzd) {
NodeList.prototype.indexOf = Array.prototype.indexOf
const arr = [1,2,3,4,5]
const arrTarget = arr[2]
const tpl = document.createElement('template')
tpl.innerHTML = `<div><div>1</div><div>2</div><div>3</div><div>4</div><div>5</div></div>`
const nl = tpl.content.firstChild.childNodes
const nlTarget = nl[2]
const nlArr = Array.from(nl)
const indexOf = Array.prototype.indexOf;
window.initzd = {
arr,
arrTarget,
nl,
nlTarget,
nlArr,
indexOf
};
}
const {arr, arrTarget, nl, nlTarget, nlArr, indexOf } = window.initzd;
};
suite.add("arr.indexOf(arrTarget)", function () {
arr.indexOf(arrTarget)
});
suite.add("nl.indexOf(nlTarget)", function () {
nl.indexOf(nlTarget)
});
suite.add("indexOf.call(nl, nlTarget)", function () {
indexOf.call(nl, nlTarget)
});
suite.add("Array.from(nl).indexOf(nlTarget)", function () {
Array.from(nl).indexOf(nlTarget)
});
suite.add("nlArr.indexOf(nlTarget)", function () {
nlArr.indexOf(nlTarget)
});
suite.add("[...nl].indexOf(nlTarget)", function () {
[...nl].indexOf(nlTarget)
});
suite.add("nl[0].nextSibling.nextSibling", function () {
nl[0].nextSibling.nextSibling
});
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("childNodes indexOf #jsbench #jsperf");
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