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', false); toggleAttributeSpacedValue1(asdf, 'data-role', 'ccc');
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', false); toggleAttributeSpacedValue2(asdf, 'data-role', 'ccc');
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):
It looks like we have a complex JavaScript function to analyze. To break it down, the function appears to be responsible for toggling the `data-role` attribute on elements with IDs `asdf1` and/or `asdf2`, while also handling spaced values. Here's my attempt to summarize the key aspects of the code: **Key Functionality:** 1. Iterates over an array of test cases, each representing a set of operations to perform on HTML elements. 2. For each test case: * Retrieves the corresponding HTML element(s) using `document.querySelector`. * Toggles the `data-role` attribute on each element based on the provided arguments (e.g., `true` for add, `false` for remove). * Handles spaced values by splitting the input string into individual values and applying them to the `data-role` attribute. 3. After processing all test cases, updates the `element` object with the final `data-role` value. **Notes:** * The function uses several regular expressions (`rAttributeCleanup`, `rSpaceGlobal`) to manipulate the input strings and HTML elements. * It also employs a technique called "spaced values" to handle multiple values for the same attribute, where spaces are used as separators. * The `toggleAttributeSpacedValue1`/`toggleAttributeSpacedValue2` functions appear to be wrappers around this core functionality. **Uncertainties:** * I'm not entirely sure how the spaced values work, as there's not much information about their specific usage or limitations in the provided code snippet. * The use of `force` and `currentAttributeValue` parameters in some test cases is unclear without more context. If you have any further questions or need help with a specific aspect of this code, feel free to ask!
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?