<script type="text/javascript" src="https://publicstatic.tableausoftware.com/vizql/v_100001608191615/javascripts/mscorlib.js"></script>
<script type="text/javascript" src="https://publicstatic.tableausoftware.com/vizql/v_100001608191615/javascripts/vqlui.debug.js"></script>
<div id="testRoot">
<span>1</span>
<span>1</span>
<span>1</span>
<span>1</span>
<span>1</span>
<span>1</span>
<span>1</span>
</div>
function CleanUpFontFamilyStyles1(node) {
var fontOjects = node.find('span');
for (var ii = 0; ii < fontOjects.length; ++ii) {
var obj = fontOjects.eq(ii);
var fontFamily = obj.css('font-family');
if (!ss.isNullOrEmptyString(fontFamily)) {
var newCss = {};
var fontFaces = [];
fontFaces.push(fontFamily);
if (ss.startsWithString(fontFamily, "'") || ss.startsWithString(fontFamily, '"')) {
fontFamily = fontFamily.substr(1, fontFamily.length - 2);
}
if (fontFaces.length > 1) {
newCss['font-family'] = fontFaces.join(',');
obj.css(newCss);
}
}
}
};
function CleanUpFontFamilyStyles2(node) {
var fontOjects = node.find('span');
fontOjects.each(function( index, obj ) {
var fontFamily = obj.style.fontFamily;
if (!ss.isNullOrEmptyString(fontFamily)) {
var newCss = {};
var fontFaces = [];
fontFaces.push(fontFamily);
if (ss.startsWithString(fontFamily, "'") || ss.startsWithString(fontFamily, '"')) {
fontFamily = fontFamily.substr(1, fontFamily.length - 2);
}
if (fontFaces.length > 1) {
newCss['font-family'] = fontFaces.join(',');
obj.css(newCss);
}
}
});
};
function CleanUpFontFamilyStyles3(node) {
var fontOjects = node.find('span');
fontOjects.each(function( index, obj ) {
var fontFamily = window.getComputedStyle(obj).fontFamily;
if (!ss.isNullOrEmptyString(fontFamily)) {
var newCss = {};
var fontFaces = [];
fontFaces.push(fontFamily);
if (ss.startsWithString(fontFamily, "'") || ss.startsWithString(fontFamily, '"')) {
fontFamily = fontFamily.substr(1, fontFamily.length - 2);
}
if (fontFaces.length > 1) {
newCss['font-family'] = fontFaces.join(',');
obj.css(newCss);
}
}
});
};
CleanUpFontFamilyStyles1($("#testRoot"));
CleanUpFontFamilyStyles2($("#testRoot"));
CleanUpFontFamilyStyles3($("#testRoot"));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Test 1 | |
Optimized | |
with getComputedStyle |
Test name | Executions per second |
---|---|
Test 1 | 10298.2 Ops/sec |
Optimized | 72364.5 Ops/sec |
with getComputedStyle | 24684.0 Ops/sec |
I'll break down the benchmark and its options for you.
Benchmark Definition:
The benchmark measures the performance of three different approaches to cleaning up font family styles in HTML elements.
Script Preparation Code:
There are two main script functions:
CleanUpFontFamilyStyles1(node)
: This function iterates over all <span>
elements within a given node (in this case, #testRoot
). For each element, it extracts the font family style and checks if it's not an empty string. If so, it creates a new CSS object with only the first character of the font family removed from both quotes. It then updates the span element's style with the new CSS.CleanUpFontFamilyStyles2(node)
: Similar to the previous function, but uses the each
method provided by jQuery to iterate over the elements.CleanUpFontFamilyStyles3(node)
: This function also iterates over all <span>
elements within a given node, but uses the getComputedStyle
API to get the font family style and then updates the span element's style.Options Compared:
The three options being compared are:
CleanUpFontFamilyStyles1
(iterating over elements using jQuery)CleanUpFontFamilyStyles2
(using the each
method provided by jQuery)CleanUpFontFamilyStyles3
(using the getComputedStyle
API)Pros and Cons of Each Approach:
CleanUpFontFamilyStyles1
:CleanUpFontFamilyStyles2
:each
.CleanUpFontFamilyStyles3
:getComputedStyle
API for getting font family styles.Other Considerations:
cleanUpFontFamilyStyles
functions are executed multiple times ( indicated by the "ExecutionsPerSecond" value), suggesting that each execution has a negligible impact on overall performance.Benchmark Results:
The latest benchmark results show that:
CleanUpFontFamilyStyles2
(optimized) performs best, with an executions per second of 72364.5078125.CleanUpFontFamilyStyles3
(with getComputedStyle) is the next closest, with approximately half the performance of the optimized version.CleanUpFontFamilyStyles1
(iterating over elements using jQuery) performs worst, with an executions per second of 10298.228515625.Overall, the results suggest that using the built-in jQuery iteration methods can provide a performance advantage over manually iterating over elements or relying on external APIs like getComputedStyle
.