CodingSlow JS speed-up.

 

Press Ctrl+Enter to quickly submit your post
Quick Reply  
 
 
  
 From:  Peter (BOUGHTONP)  
 To:  ALL
32013.1 
The following function is /SLOW/ - between half a second to a second for just three iterations.

I've already added the stylesheet filtering which has helped a bit, but it's still looping through 53 declarations (rather than just directly changing the relevant one)

Anyone know how to speed it up?

JavaScript code:
function changecss (sheet, theClass, element, value)
{
	var result = false;
 
	var cssRules;
	if (document.all) cssRules = 'rules';
	else if (document.getElementById) cssRules = 'cssRules';
 
	var ss = -(sheet.length+4)
	var sl = -4;
 
	for (var S = 0; S < document.styleSheets.length; S++)
	{
		if (document.styleSheets[S].href.slice(ss,sl) == sheet)
		{
			var thisRules = document.styleSheets[S][cssRules];
			// loops through styles in sheet to find the correct one.
			for (var R = 0; R < thisRules.length; R++)
			{
				if (thisRules[R].selectorText == theClass)
				{
					thisRules[R].style[element] = value;
					result = true;
				}
			}
			if (sheet != '')
			{
				break;
			}
		}
	}
 
	return result;
}

(I'd like to avoid changing the className of the actual elements, since there's lots of elements compared to just a handful of actual classes used)
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  ALL
32013.2 
Oh, and it looks like this is an IE6 issue - it runs in under 50ms with FF2.
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  ALL
32013.3 
Decided to give up on it, as it's only for relatively minor functionality.

(But feel free to still make suggestions if you really want)
0/0
 Reply   Quote More 

 From:  Ben (BENLUMLEY)  
 To:  Peter (BOUGHTONP)     
32013.4 In reply to 32013.3 
i sometimes make something trivial work, and realise i should have come to this conclusion several hours earlier!
0/0
 Reply   Quote More 

 From:  Rich  
 To:  Peter (BOUGHTONP)     
32013.5 In reply to 32013.3 
Lose the for loop and program each iteration manually?

Rich - e - w - m
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  Rich     
32013.6 In reply to 32013.5 
Which one?

The outer one is required to identify the stylesheet, and the position might change as different sheets are added/removed, so it can't be fixed.

Likewise, the inner loop is required to identify the declaration, which again is not necessarily in a fixed position.
0/0
 Reply   Quote More 

Reply to All    
 

1–6

Rate my interest:

Adjust text size : Smaller 10 Larger

Beehive Forum 1.5.2 |  FAQ |  Docs |  Support |  Donate! ©2002 - 2024 Project Beehive Forum

Forum Stats