Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Compare toggling attribute spaced values
(version: 0)
Comparing performance of:
V1 vs V2
Created:
8 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id="asdf1"></div> <div id="asdf2"></div>
Script Preparation code:
function toggleAttributeSpacedValue2(elements, attributeName, value, force) { if (elements && typeof elements == 'object') { var i = 0, eln = typeof elements.length == 'number', el = eln ? elements.length : 1, vl, vln, values, finalAttributeValue, currentAttributeValue, valueItem, spacedValueItem, hasValueItem, element; if (value && (typeof value == 'string' || (value = value + ''))) { for (; i < el; i++) { if (typeof (element = eln ? elements[i] : elements) == 'object' && element.nodeType === 1) { finalAttributeValue = currentAttributeValue = (currentAttributeValue = element.getAttribute(attributeName)) ? ' ' + currentAttributeValue.replace(rSpaceGlobal, ' ') + ' ' : ''; values || ((vln = value.indexOf(' ') > -1) ? vl = (values = value.split(rSpaceGlobal)).length : vl = 1, values = value.trim()); for (var j = 0; j < vl; j++) { if ((valueItem = vln ? values[j] : values)) { hasValueItem = !finalAttributeValue ? false : finalAttributeValue.indexOf(spacedValueItem = ' ' + valueItem + ' ') > -1; if (typeof force != 'boolean' || force != hasValueItem) { finalAttributeValue = hasValueItem ? finalAttributeValue.replace(spacedValueItem, ' ') : finalAttributeValue + valueItem + ' '; } } } if (finalAttributeValue && (finalAttributeValue = finalAttributeValue.trim())) { element.setAttribute(attributeName, finalAttributeValue); } else { element.removeAttribute(attributeName); } } } } else { for (; i < el; i++) { if (typeof (element = eln ? elements[i] : elements) == 'object' && element.nodeType === 1) { element.removeAttribute(attributeName); } } } } } var rSpaceGlobal = /\s+/g, toggleAttributeSpacedValue1 = (function() { var rSpace = /\s+/, rSpaceGlobal = /\s+/g, rAttributeCleanup = /[\n\t\r]/g; return function(elements, attributeName, value, force) { if (elements && typeof elements == 'object') { if (typeof elements.length != 'number') { elements = [elements]; } if (typeof force != 'boolean') { force = undefined; } if (value && (typeof value == 'string' || (value = value + ''))) { var values, attributeValue, finalAttributeValue, currentAttributeValue, valueItem, spacedValueItem, hasValueItem, remove, element; for (var i = 0, el = elements.length; i < el; i++) { element = elements[i]; if (typeof element == 'object' && element.nodeType === 1) { attributeValue = element.getAttribute(attributeName); finalAttributeValue = currentAttributeValue = attributeValue ? (' ' + attributeValue + ' ').replace(rAttributeCleanup, ' ') : ''; for (var j = 0, vl = (values || (values = value.split(rSpace))).length; j < vl; j++) { if ((valueItem = values[j])) { spacedValueItem = ' ' + valueItem + ' '; hasValueItem = ~finalAttributeValue.indexOf(spacedValueItem); remove = force != undefined ? !force : hasValueItem; if (remove && hasValueItem) { finalAttributeValue = finalAttributeValue.replace(spacedValueItem, ' ') } else if (!remove && !hasValueItem) { finalAttributeValue = finalAttributeValue + spacedValueItem; } } } if (finalAttributeValue != currentAttributeValue) { finalAttributeValue = finalAttributeValue.replace(rSpaceGlobal, ' '); finalAttributeValue = finalAttributeValue ? finalAttributeValue.trim() : finalAttributeValue; if (finalAttributeValue) { if (attributeName==='class'){ element.className = finalAttributeValue; }else{ element.setAttribute(attributeName, finalAttributeValue); } } else { element.removeAttribute(attributeName); } } } } } else { for (var i = 0, el = elements.length; i < el; i++) { element = elements[i]; if (typeof element == 'object' && element.nodeType === 1) { element.removeAttribute(attributeName); } } } } } })()
Tests:
V1
var asdf = document.querySelector('asdf1'); toggleAttributeSpacedValue1(asdf, 'data-role', 'aaa'); toggleAttributeSpacedValue1(asdf, 'data-role', 'aaa', true); toggleAttributeSpacedValue1(asdf, 'data-role', 'bbb', false); toggleAttributeSpacedValue1(asdf, 'data-role', 'bbb'); toggleAttributeSpacedValue1(asdf, 'data-role', 'ccc'); toggleAttributeSpacedValue1(asdf, 'data-role', 'bbb'); toggleAttributeSpacedValue1(asdf, 'data-role', 'aaa ccc', false);
V2
var asdf = document.querySelector('asdf2'); toggleAttributeSpacedValue2(asdf, 'data-role', 'aaa'); toggleAttributeSpacedValue2(asdf, 'data-role', 'aaa', true); toggleAttributeSpacedValue2(asdf, 'data-role', 'bbb', false); toggleAttributeSpacedValue2(asdf, 'data-role', 'bbb'); toggleAttributeSpacedValue2(asdf, 'data-role', 'ccc'); toggleAttributeSpacedValue2(asdf, 'data-role', 'bbb'); toggleAttributeSpacedValue2(asdf, 'data-role', 'aaa ccc', false);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
V1
V2
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided code and test cases to provide an answer. **Code Analysis** The code is written in JavaScript and appears to be a benchmarking script for testing the performance of two functions: `toggleAttributeSpacedValue1` and `toggleAttributeSpacedValue2`. The functions seem to modify attributes on HTML elements using spaced values. The main difference between the two functions is that `toggleAttributeSpacedValue1` modifies attributes on specific elements (`asdf1`) and `toggleAttributeSpacedValue2` modifies attributes on all elements (`asdf2`). **Test Cases** There are two test cases: "V1" and "V2". Both test cases involve modifying the same set of attributes on different HTML elements. The main difference between the test cases is that they use different versions of the `toggleAttributeSpacedValue` functions. **Benchmark Result** The latest benchmark result shows the execution rate per second for each function and browser version. The results indicate that: * For Chrome 52, `V2` has a higher execution rate than `V1`. * Both test cases have similar performance on Windows devices. Based on this analysis, I would answer that there is no clear winner between the two functions (`toggleAttributeSpacedValue1` and `toggleAttributeSpacedValue2`) in terms of performance. However, if I had to make an educated guess, I would say that `V2` might have a slight edge due to its more aggressive attribute modification strategy. Please note that this answer is based on the provided code and test cases only, and actual performance differences may vary depending on various factors such as HTML structure, network conditions, and system resources.
Related benchmarks:
Settings attributes as list
Comparing pure setAttribute vs if/else setAttribute/className
get all element's attributes
Setting the same value with setAttribute()
Lopp over element attributes
Comments
Confirm delete:
Do you really want to delete benchmark?