Setting CSS props and timers from JavaScript

Koos Vriezen koos.vriezen at xs4all.nl
Sun Nov 3 14:17:09 GMT 2002


Hi,

Looking why a page was eating all my CPU:
  function makeStatic() {
    if (NS||NS6) {winY = window.pageYOffset;}
    if (IE) {winY = document.body.scrollTop;}
    if (NS6||IE||NS) {
    if (winY!=lastY&&winY>YOffset-staticYOffset) {
      smooth = .2 * (winY - lastY - YOffset + staticYOffset);}
    else if (YOffset-staticYOffset+lastY>YOffset-staticYOffset) {
      smooth = .2 * (winY - lastY - (YOffset-(YOffset-winY)));}
    else {smooth=0}
    if(smooth > 0) smooth = Math.ceil(smooth);
    else smooth = Math.floor(smooth);
    if (IE) bssm.pixelTop+=smooth;
    if (NS6||NS) bssm.top=parseInt(bssm.top)+smooth
    lastY = lastY+smooth;
    setTimeout('makeStatic()', 1)}}
where bssm is a div.style.

I found that KHTML doesn't limit the timeout value. Running a page like
    <html><head><script>
            var count = 0;
            var stop = false;
            function startTimer1() {
                count++;
                if (!stop)
                    setTimeout("startTimer1()", 0);
            }
            function startTimers() {
                setTimeout("startTimer1()", 0);
                setTimeout("stop=true;alert(count)", 3000);
            }
    </script></head><body onLoad="startTimers()"></body></html>
KHTML ends with 12538 on my box while eating all CPU. Mozilla ends with
185, probably has a limit of 16ms.
It's quite easy to set a limit in WindowQObject::installTimeout (2 times).

Another thing is that setting the top property. Now a property is first
removed and than added, marking it as changed trggering a recalcStyle.
Adding 'if (styleDecl.getPropertyValue(prop) == propvalue) return;'
in DOMCSSStyleDeclaration::tryPut does seem to prevent this (but may slow
down cases where propvalue != current value).

Regards,

Koos Vriezen
-------------- next part --------------
Index: ecma/kjs_window.cpp
===================================================================
RCS file: /home/kde/kdelibs/khtml/ecma/kjs_window.cpp,v
retrieving revision 1.288
diff -u -3 -p -r1.288 kjs_window.cpp
--- ecma/kjs_window.cpp	2002/11/02 21:23:07	1.288
+++ ecma/kjs_window.cpp	2002/11/03 14:14:33
@@ -1476,6 +1476,7 @@ void WindowQObject::parentDestroyed()
 int WindowQObject::installTimeout(const UString &handler, int t, bool singleShot)
 {
   //kdDebug(6070) << "WindowQObject::installTimeout " << this << " " << handler.ascii() << endl;
+  if (t < 16) t = 16;
   int id = startTimer(t);
   ScheduledAction *action = new ScheduledAction(handler.qstring(),singleShot);
   scheduledActions.insert(id, action);
@@ -1486,6 +1487,7 @@ int WindowQObject::installTimeout(const 
 int WindowQObject::installTimeout(const Value &func, List args, int t, bool singleShot)
 {
   Object objFunc = Object::dynamicCast( func );
+  if (t < 16) t = 16;
   int id = startTimer(t);
   scheduledActions.insert(id, new ScheduledAction(objFunc,args,singleShot));
   return id;
Index: ecma/kjs_css.cpp
===================================================================
RCS file: /home/kde/kdelibs/khtml/ecma/kjs_css.cpp,v
retrieving revision 1.56
diff -u -3 -p -r1.56 kjs_css.cpp
--- ecma/kjs_css.cpp	2002/10/25 10:21:25	1.56
+++ ecma/kjs_css.cpp	2002/11/03 14:14:33
@@ -175,6 +175,8 @@ void DOMCSSStyleDeclaration::tryPut(Exec
 #ifdef KJS_VERBOSE
     kdDebug(6070) << "DOMCSSStyleDeclaration: prop=" << prop << " propvalue=" << propvalue << endl;
 #endif
+    if (styleDecl.getPropertyValue(prop) == propvalue)
+        return;
     styleDecl.removeProperty(prop);
     if(!propvalue.isEmpty())
     {


More information about the kfm-devel mailing list