/    Sign up×
Community /Pin to ProfileBookmark

slow performance in firefox

first of hi to all since i’m new here. now to my question.
i have a script that i use to dynamically create div element and textbox inside. now, when div is created i use sort of a zoom effect to stretch it form 0px width and 0px height to desired width and height (stretching is done with centering so that effect looks like div is coming from center of page). script that i have written works fine in all browsers, just in firefox zooming is slow (it lags) while in ie and safari it is nice and smooth (and i have no idea why is that since this is all basic arithmetics). how can i improve it so that it works fine in firefox also? here is the code. i commented parts of code so that it would be easier to figure out what happens where since i used croatian names:

[code=php]function cmd(){
this.element = null;
this.terminal = null;
this.stvarnaVisina = 0;
this.stvarnaSirina = 0;
this.faktorPovecanja = 0;
this.faktorProsirenja = 0;
this.timer = null;
this.terminalPrefiks = null;

//here i create div with textarea inside
this.stvori = function(){
this.terminalPrefiks = new String(‘~ dir.hr: ‘);
this.element = document.createElement(‘DIV’);
this.element.id = ‘CMD’;
this.element.className = ‘terminal’;
this.element.innerHTML = ‘<table><tr><td class=”cmd-lijevo”>&nbsp;</td><td class=”cmd-sredina”>Terminal</td>’ +
‘<td id=”zatvori” class=”cmd-desno” onClick=”cmd.unisti();”>&nbsp;</td></tr><tr><td colspan=”3″>’ +
‘<textarea onSelect=”cmd.clearExtraSelection();” id=”terminalTextArea”>’+this.terminalPrefiks+'</textarea></td></tr></table>’;
document.body.appendChild(this.element);
this.terminal = document.getElementById(‘terminalTextArea’);
this.doSlowOpen();
}

this.unisti = function(){
this.terminal = null;
document.body.removeChild(this.element);
this.element = null;
this.stvarnaVisina = 0;
this.stvarnaSirina = 0;
this.faktorPovecanja = 0;
this.faktorProsirenja = 0;
this.timer = null;
}

this.doSlowOpen = function(){
//sets final width of div to window width – 50 px and final height to window height – 50 px
if(window.innerWidth){
this.stvarnaSirina = window.innerWidth – 50;
this.stvarnaVisina = window.innerHeight – 50;
}
else if(document.body.offsetWidth){
this.stvarnaSirina = document.body.offsetWidth – 50;
this.stvarnaVisina = document.body.offsetHeight – 50;
}
//set resize step to 1/20 of final width and height of div – interval will be repeated for 20 times
this.faktorProsirenja = parseInt(this.stvarnaSirina / 20);
this.faktorPovecanja = parseInt(this.stvarnaVisina / 20);
//set div to 0px*0px
this.element.style.height = ‘0px’;
this.element.style.width = ‘0px’;
var _this = this;
//call function that dose actual resize
this.timer = setInterval(function(){_this.slowOpen();}, 40);
//this is just to set focus on textarea
setTimeout(function(){_this.focus();},1);
}

this.slowOpen = function(){
if(this.element.offsetHeight < this.stvarnaVisina){
if((this.element.offsetHeight + this.faktorPovecanja) < this.stvarnaVisina){
//resize element by factor calculated before
this.element.style.height = this.element.offsetHeight + this.faktorPovecanja + “px”;
this.element.style.width = this.element.offsetWidth + this.faktorProsirenja + “px”;
//center element in the page
if(window.innerWidth){
this.element.style.top = Math.round(((screen.height – (screen.height – window.innerHeight) – this.element.offsetHeight) / 2)) + ‘px’;
this.element.style.left = Math.round(((window.innerWidth – this.element.offsetWidth) / 2)) + ‘px’;
}
else if(document.body.offsetWidth){
this.element.style.left = Math.round(((document.body.offsetWidth – this.element.offsetWidth) / 2)) + ‘px’;
this.element.style.top = Math.round(((screen.height – (screen.height – document.body.offsetHeight) – this.element.offsetHeight) / 2)) + ‘px’;
}
}
else{
//this is just in case if element is over resized to put it in correct size and center it
this.element.style.height = this.stvarnaVisina;
this.element.style.width = this.stvarnaSirina;
if(window.innerWidth){
this.element.style.top = Math.round(((screen.height – (screen.height – window.innerHeight) – this.element.offsetHeight) / 2)) + ‘px’;
this.element.style.left = Math.round(((window.innerWidth – this.element.offsetWidth) / 2)) + ‘px’;
}
else if(document.body.offsetWidth){
this.element.style.left = Math.round(((document.body.offsetWidth – this.element.offsetWidth) / 2)) + ‘px’;
this.element.style.top = Math.round(((screen.height – (screen.height – document.body.offsetHeight) – this.element.offsetHeight) / 2)) + ‘px’;
}
}
}
else{
clearInterval(this.timer);
}
}

this.focus = function(){
this.terminal.focus();
if(this.terminal.setSelectionRange){
this.terminal.setSelectionRange(parseInt(this.terminal.value.length), parseInt(this.terminal.value.length));
}
else{
var range = this.terminal.createTextRange();
range.collapse(true);
range.moveStart(‘character’,parseInt(this.terminal.value.length));
range.moveEnd(‘character’,parseInt(0));
range.select();
}
}
}[/code]

thanx for any help and sorry for my lousy english

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@zvoneauthorOct 02.2007 — to demonstrate the problem here's a little movie how that looks:

http://www.ffri.hr/~zmartin/test.mov

movie has 66kb

you will see that that layer open less than acceptably slow, but on ie it goes fast and smooth as one would expect
Copy linkTweet thisAlerts:
@zvoneauthorOct 03.2007 — anyone?! ?

i guess it's something elementary and stupid, but i can't figure it out
Copy linkTweet thisAlerts:
@WebnerdOct 03.2007 — One thing, you are calculating EVERY single item. You should preset certain values at the beginning of the script. Such as :

this.contentwid=document.body.offsetWidth;

this.screenwid=screen.width;

Firefox on a Mac has some serious memory leaks. Unless someone has some other advice, I would just made those minor changes above.
Copy linkTweet thisAlerts:
@zvoneauthorOct 03.2007 — One thing, you are calculating EVERY single item. You should preset certain values at the beginning of the script. Such as :

this.contentwid=document.body.offsetWidth;

this.screenwid=screen.width;[/QUOTE]

ok, i tried with that, but that didn't help as i thought. thing is that i calculate just width, height and left and top position of that div with this:
[code=php]this.element.style.width = this.element.offsetWidth + this.faktorProsirenja + "px";
this.element.style.height = this.element.offsetHeight + this.faktorPovecanja + "px";
if(window.innerWidth){
this.element.style.left = Math.round(((this.sirinaEkrana - this.element.offsetWidth) / 2)) + 'px';
this.element.style.top = Math.round(((this.visinaEkrana - this.element.offsetHeight) / 2)) + 'px';
}
else{
this.element.style.left = Math.round(((this.sirinaEkrana - this.element.offsetWidth) / 2)) + 'px';
this.element.style.top = Math.round(((this.visinaEkrana - this.element.offsetHeight) / 2)) + 'px';
}[/code]

sirinaEkrana and visinaEkrana i calculate before (like you mentioned) with this:
[code=php]if(window.innerWidth){
this.stvarnaSirina = window.innerWidth - 50;
this.stvarnaVisina = window.innerHeight - 50;
this.sirinaEkrana = window.innerWidth;
this.visinaEkrana = screen.height - (screen.height - window.innerHeight);
}
else if(document.body.offsetWidth){
this.stvarnaSirina = document.body.offsetWidth - 50;
this.stvarnaVisina = document.body.offsetHeight - 50;
this.sirinaEkrana = document.body.offsetWidth;
this.visinaEkrana = screen.height - (screen.height - document.body.offsetHeight);
}[/code]

so as you can see this is all just basic arithmetics - addition, subtraction and division plus some string concatenation.
Firefox on a Mac has some serious memory leaks. Unless someone has some other advice, I would just made those minor changes above.[/QUOTE]
funny thing, i looked at cpu and memory usage (on mac) while running that code and cpu usage goes up to 60% and memory usage increases by 10 mb. all dough that is quite a lot it still doesn't explain why it is so slow.

now i have to mention that this is on mac mini (g4 1.25ghz and 512 mb of ram), but same performance i have on my pc (p4 2.4ghz and 1.5gb of ram) so it is not just on firefox on mac.

plus, if you looked at that mov i posted you will notice some miscalculation in firefox at the end of execution. entire layer is few pixels right than it should be, but in ie is perfect.
Copy linkTweet thisAlerts:
@toicontienOct 03.2007 — Some things to watch out for:

  • 1. The interval is only 40 miliseconds. That essentially means your animation is running at 25 frames per second. Try reducing that to 18 frames per second, or 56 miliseconds. The number of times that must be calculated can be a big drag on the processor.


  • 2. The visual complexity of the Web page makes a HUGE difference in how fast animations run in Firefox. Firefox, and all Gecko based browsers, take a lot of processor cycles to re-render elements on screen. This becomes apparent when your page has a lot of background and embedded images. The page you have in the movie doesn't seem to be too busy. Also, the area of the screen that gets re-rendered with each animation frame also plays a big roll in the overall smoothness of the animation.


  • 3. Firefox blows when it comes to animations. It's just slow. You need a pretty beefy graphics card for things to really run smoothly. Try that page in Safari. I bet the animation would be smooth as a frosty brew on a hot day.


  • The biggest thing you can do is reduce the frame rate of the animation -- that is use a lower time for the setInterval function. About 56 milliseconds is 18 frames per second. Try that first.
    Copy linkTweet thisAlerts:
    @zvoneauthorOct 03.2007 — well, i finaly got to the bottom of this. problem was in layers opacity. aparently firefox dosen't work well if you do some effects like this on a layer that has opacity. removing opacity from css solved the problem.

    well, when mozilla takes care of this i'll turn it on again till then its opacity just for ie.

    thanx toicontien for all insights!
    Copy linkTweet thisAlerts:
    @toicontienOct 04.2007 — On that note too, I read that Firefox 3 will be using the same script interpreter that Flash uses, so hopefully JavaScript performance will be blazing fast in Firefox 3. I'll be happy as soon as the other browsers chew through JavaScript as quickly as Safari. That browser just amazes me with its speed. Firefox 3 will probably be comparable.
    ×

    Success!

    Help @zvone spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...