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
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<div class="textfill" style="width:100px; height:50px; font-size: 10px;">
<span>
My Text Here dsjf fsajdk fhj jfksad fkas fkjsadf kasdj hkjfashfd klah kfjlasd kjf
hf kajsdlfa
</span>
</div>
// by GeekyMonkey
$.fn.textfill1 = function(options) {
var fontSize = options.maxFontPixels;
var ourText = $('span:visible:first', this);
var maxHeight = $(this).height();
var maxWidth = $(this).width();
var textHeight;
var textWidth;
do {
ourText.css('font-size', fontSize);
textHeight = ourText.height();
textWidth = ourText.width();
fontSize = fontSize - 1;
} while ((textHeight > maxHeight || textWidth > maxWidth) && fontSize > 3);
return this;
}
// by Alexander Sandstorm
$.fn.textfill2 = function(options) {
options = jQuery.extend({
maxFontSize: null,
minFontSize: 8,
step: 1
}, options);
return this.each(function() {
var innerElements = $(this).children(':visible'),
fontSize = options.maxFontSize || innerElements.css("font-size"),
// use current font-size by default
maxHeight = $(this).height(),
maxWidth = $(this).width(),
innerHeight, innerWidth;
do {
innerElements.css('font-size', fontSize);
// use the combined height of all children, eg. multiple <p> elements.
innerHeight = $.map(innerElements, function(e) {
return $(e).outerHeight();
}).reduce(function(p, c) {
return p + c;
}, 0);
innerWidth = innerElements.outerWidth(); // assumes that all inner elements have the same width
fontSize = fontSize - options.step;
} while ((innerHeight > maxHeight || innerWidth > maxWidth) && fontSize > options.minFontSize);
});
};
// by mekwall
$.fn.textfill3 = function(maxFontSize) {
maxFontSize = parseInt(maxFontSize, 10);
return this.each(function() {
var ourText = $("span", this),
parent = ourText.parent(),
maxHeight = parent.height(),
maxWidth = parent.width(),
fontSize = parseInt(ourText.css("fontSize"), 10),
multiplier = maxWidth / ourText.width(),
newSize = (fontSize * (multiplier - 0.1));
ourText.css("fontSize", (maxFontSize > 0 && newSize > maxFontSize) ? maxFontSize : newSize);
});
}
var ourText = $(".textfill");
// reset the size so we can calculate again
ourText.find("span").css("fontSize", "");
Ready to run.
Test | Ops/sec | |
---|---|---|
textfill by GeekyMonkey |
| ready |
textfill by Alexander Sandstorm |
| ready |
textfill by mekwall |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.