RobB wrote:
[...]
That would seem to be the operant question: when does
pants-and-suspenders (and cummerbund & drawstring) object testing of
Belt and braces? ;-)
this sort become a 'burden', occupying a significant volume of the
codebase? I've never seen a script which intentionally enhanced
functionality by patching in getElementById in user agents which did
not implement Style objects, although I suppose anything is possible.
Should everything be tested for before assuming its implementation?
When may asumptions be made?
A good question and worth pondering. However, before navel gazing,
lets look at the practicalities. I put each of the following into a
separate function, then called them 1000 times from separate functions,
hopefully avoiding any conflicts between the two:
if (document.getElementById) {var x = document.getElementById('rob')}
versus:
var x = document.getElementById('rob');
for 1,000 iterations took approximately 30 ms and 15 ms respectively -
though in Firefox the tested script sometimes ran in 15 ms also. IE
gave similar but less consistent results. The tested script sometimes
ran in 47ms, and the untested one often took 30ms.
So even testing a feature every time it's used will likely cause an
insignificant delay in program execution (I would think that anyone
calling gEBI 1000 times has more issues with program design in general
than with optimising their feature testing).
Therefore given the minimal overhead that thorough testing costs, why
not go for thoroughness and elegance within reason and just do it?
PS.
I also took the time to test using an array length versus setting a
variable in the for loop, e.g.:
for (var i=0; i< z.length, i++)
versus
for (var i=0, len=z.length; i<len; i++)
There was no measurable difference between the two.
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><title>demo</title>
<script type="text/javascript">
function testYes() {
if (document.getElementById) {
var x = document.getElementById('rob1');
}
}
function testNo() {
var x = document.getElementById('rob2');
}
function runTest1() {
var now0 = new Date();
for ( var i=0; i<1000; i++ ){
testYes();
}
var now1 = new Date();
var elapsed = now1.getTime() - now0.getTime();
alert('Feature tested: ' + elapsed);
}
function runTest2() {
var now0 = new Date();
for ( var i=0; i<1000; i++ ){
testNo();
}
var now1 = new Date();
var elapsed = now1.getTime() - now0.getTime();
alert('Not feature tested: ' + elapsed);
}
</script>
</head>
<body>
<div id="rob1" onclick="runTest1();">tested</div>
<div id="rob2" onclick="runTest2();">untested</div>
</body>
</html>
--
Rob