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
window.cachedCanvas = null;
window.cachedContext = null;
function measureTextWidthInPixelsOriginal(size, text) {
if (typeof document === 'undefined') {
return 0;
}
if (!cachedCanvas) {
cachedCanvas = document.createElement('canvas');
try {
cachedContext = cachedCanvas.getContext('2d');
} catch (e) {
cachedContext = null;
}
}
if (!cachedContext) {
return 0;
}
cachedContext.font = `${size}px system-ui`;
const w = cachedContext.measureText(text).width;
return w;
}
function measureTextWidthInPixelsDesync(size, text) {
if (typeof document === 'undefined') {
return 0;
}
if (!cachedCanvas) {
cachedCanvas = document.createElement('canvas', {
alpha: false,
desynchronized: true,
willReadFrequently: true,
});
try {
cachedContext = cachedCanvas.getContext('2d');
} catch (e) {
cachedContext = null;
}
}
if (!cachedContext) {
return 0;
}
cachedContext.font = `${size}px system-ui`;
const w = cachedContext.measureText(text).width;
return w;
}
function measureTextWidthInPixelsOffscreen(size, text) {
if (typeof document === 'undefined') {
return 0;
}
if (!cachedCanvas) {
cachedCanvas = new OffscreenCanvas(1, 1);
try {
cachedContext = cachedCanvas.getContext('2d');
} catch (e) {
cachedContext = null;
}
}
if (!cachedContext) {
return 0;
}
cachedContext.font = `${size}px system-ui`;
const w = cachedContext.measureText(text).width;
return w;
}
Ready to run.
Test | Ops/sec | |
---|---|---|
original |
| ready |
desync |
| ready |
offscreen |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.