From: Peroli on
On Apr 22, 10:22 pm, "Evertjan." <exjxw.hannivo...(a)interxnl.net>
wrote:
> Peroli wrote on 22 apr 2008 in comp.lang.javascript:
>
> > function getSelectedValueForName(name) {
> > var inputs = document.getElementsByTagName("input");
> > for (var i = 0, len = inputs.length; i < len; i++) {
> > if (inputs[i].type == "radio" && input[i].checked) {
> > return input[i].value;
> > }
> > }
> > return null;
> >}
>
> Where is the parameter called name used?
>
> If the inputs have a common name in a form do:
>
> var inputs = document.forms['myForm'].elements[name];
>
> --
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)

Evertjan,
I agree. I will test my code before posting from now on.

Tim,
I agree to your first point that using inputs[i] 3 times is not
efficient.
In-fact, my test results showed that its slower by 10 - 30%.
I wrote a small test script and found that $$ is indeed faster in
Firefox and Safari.
Haven't tested it in IE, but I know it will be slower, because $$
uses native xpath.
Evaluate my test script and share your suggestions. I am open to
criticism.

--- Script (uses prototype.js) ---

<html>
<head>
<title>Benchmark</title>
</head>
<body>
<form id="test_form">
<input type="button" id="getValues" value="getValues">
</form>
<span id="result" />
<script type="text/javascript" src="http://prototypejs.org/assets/
2008/1/25/prototype-1.6.0.2.js"></script>
<script type="text/javascript">
function getSelectedValueForName(name) {
for (var i = 0; i <
document.getElementsByTagName(name).length; i++) {
var input = document.getElementsByTagName(name)[i];
if (input.type == "radio" && input.checked) {
return input.value;
}
}
return null;
}

function getSelectedValueForName1(name) {
var inputs = document.getElementsByTagName(name);
for (var i = 0, len = inputs.length; i < len; i++) {
if (inputs[i].type == "radio" && inputs[i].checked) {
return inputs[i].value;
}
}
return null;
}

function getSelectedValueForName2(name) {
var inputs = document.getElementsByTagName(name);
for (var i = 0, len = inputs.length; i < len; i++) {
var input = inputs[i];
if (input.type == "radio" && input.checked) {
return input.value;
}
}
return null;
}

function getSelectedRadioValue() {
return $$('input:checked[type="radio"]').pluck('value');
}

function benchmark() {
var d = new Date();
(100).times(
function() {
var vals = getSelectedValueForName('input');
}
);
var t1 = (new Date()) - d;
d = new Date();
(100).times(
function() {
var vals = getSelectedValueForName1('input');
}
);
var t2 = (new Date()) - d;
d = new Date();
(100).times(
function() {
var vals = getSelectedValueForName2('input');
}
);
var t3 = (new Date()) - d;
d = new Date();
(100).times(
function() {
var vals = getSelectedRadioValue();
}
);
var t4 = (new Date()) - d;
$('result').update( "Tim's way:" + t1 + '<br />caching1:' + t2 +
'<br />caching2:' + t3 + '<br /> $$:' + t4);
}

function init() {
var frm = $('test_form');
(1000).times(
function (count) {
frm.insert({bottom:'<input type="radio" name="radio' +
count + '" value="value' + count + '" />'});
}
);

Event.observe('getValues', 'click', benchmark);
}
Event.observe(window, 'load', init);
</script>
</body>
</html>


-- Peroli Sivaprakasam