jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
<span id="aeolus-text-ruler"></span>
<canvas id="aeolus-text-canvas-ruler"></canvas>
var fontFamily = ''
var getFontFamily = () => {
fontFamily = getComputedStyle(document.body).fontFamily
}
getFontFamily()
var calcTextVisualWidth = function (text, fontSize) {
var ruler = document.getElementById('aeolus-text-ruler');
if (!ruler) {
ruler = document.createElement('span');
ruler.setAttribute('id', 'aeolus-text-ruler');
ruler.setAttribute('style', 'visibility: hidden; white-space: nowrap; position: fixed; bottom: -999px;');
document.body.appendChild(ruler);
}
if (ruler.style.fontSize !== "".concat(fontSize, "px"))
ruler.style.fontSize = "".concat(fontSize, "px");
ruler.innerText = text;
return ruler.offsetWidth;
};
var calcTextVisualWidth2 = function (text, fontSize) {
var canvas = document.getElementById('aeolus-text-canvas-ruler');
var context = canvas.getContext('2d');
context.font = "".concat(fontSize, "px") + ' ' + fontFamily;
var metrics = context.measureText(text);
return metrics.width;
};
Ready to run.
Test | Ops/sec | |
---|---|---|
use dom |
| ready |
use canvas |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.