Last active
July 31, 2019 13:37
-
-
Save warpech/8269752a98dee94f58319328a14bf8a7 to your computer and use it in GitHub Desktop.
getBoundingClientRect vs offsetWidth/offsetHeight (http://jsbench.github.io/#8269752a98dee94f58319328a14bf8a7) #jsbench #jsperf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>getBoundingClientRect vs offsetWidth/offsetHeight</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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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(!document.querySelector("#myDiv")) { | |
const div = document.createElement('div'); | |
div.style.display = 'inline-block'; | |
div.setAttribute('id', 'myDiv'); | |
const textNode = document.createTextNode('_'); | |
div.appendChild(textNode); | |
document.body.appendChild(div); | |
} | |
const myDiv = document.querySelector("#myDiv"); | |
const myTextNode = myDiv.firstChild; | |
}; | |
suite.add("window.foo = !window.foo;", function () { | |
window.foo = !window.foo; | |
const rect = myDiv.getBoundingClientRect(); | |
myTextNode.data = `${window.foo} ${rect.width} ${rect.height}`; | |
}); | |
suite.add("window.foo = !window.foo;", function () { | |
window.foo = !window.foo; | |
const width = myDiv.offsetWidth; | |
const height = myDiv.offsetHeight; | |
myTextNode.data = `${window.foo} ${width} ${height}`; | |
}); | |
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("getBoundingClientRect vs offsetWidth/offsetHeight"); | |
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