phpBB

Development Discussion Board

phpBB's testing ground of bleeding edge code
Advanced search

To use defer or not to use defer

General discussion of development ideas and the approaches taken in the 3.x branch of phpBB. The next feature release of phpBB 3 will be 3.1/Ascreaus followed by 3.2/Arsia.

Should we implement this or not?

We should implement!
0
No votes
I think it's a good idea to implement but if it's not implemented I don't really care.
0
No votes
Implementing this seem like a bad idea, but if someone implements I will accept.
1
33%
We shouldn't implement, bad idea.
2
67%
I abstain
0
No votes
 
Total votes: 3

To use defer or not to use defer

Postby brunoais » Sat Feb 25, 2012 12:25 am

Some time ago (almost 1 month ago) I created an RFC proposing to usage of defer in all script tags in phpbb.
The reason behind this is actually very simple. Reduce significantly the loading time of the page (time passed from the page request until the "load" or "onload" event).
By the HTML (3,4 and 5) spec, while loading a page, when a script tag is found all page processing should stop and the script should be downloaded and executed before continuing the processing of the page. In a idea to solve this problem, IE introduced the @defer attribute which meant to solve this problem. The HTML4 picked this up and introduced defer in HTML4. Unfortunately the HTML4 spec does not define what should be done when defer is found. According the the w3c (HTML4) the defer is just a clue that the browser can continue parsing the page and execute the script later. All browsers follow the HTML4 spec about defer. Each implement their way.
Webkit (chrome, chromonium, safary...) and gecko (firefox, etc...) implement the same way. Load the scripts immediately and execute them just before the load event. It was also allowed for inline script tags.
Trident (IE) implements in such way that scripts are seperated in 3 categories, inline defered, same origin external defered and other origin external defered. I've never found the order they execute between them but, for each group, they are executed the same order they appear in the page.
Presto (Opera) also took another approach dealing with defer that is also valid against HTML4's spec. It just ignores defer all together. There's the "hidden" option "Delayed Script Execution" in about:config but it's still experimental so it does not count.
At the moment only Trident 6.0 (IE10), gecko and webkit (any still supported version on both) support defer according to HTML5's spec.
Trident <6.0 (IE<10) follows the same rule for the previous versions. Opera still ignores the @defer.

With so many troubles... why use defer?
Well, IE has the <![if !IE]> and <!-- <![if IE]--> we can have the only other origin script (jQuery) load without defer for IE and for the other scirpt tags apply to IE the same way we apply the other browsers. IE has it's uncomplicated fix. All scripts are deferred except for the external jQuery.
Opera ignores defer, so if we assume that the script tags have and don't have defer. This can easily be solved using jQuery's $(document).ready() method or the DOMDocumentLoaded in addEventListener() (or the later load/onload event).
Also gecko + webkit are the owners of about 60% of the browser share (data from december 2011) and gecko + webkit + trident have about 90% of the browser share.

What do you have to say? Should we implement or not implement?

Another problem. Some recommendations say scripts should be placed at the bottom of the page. The rationale behind this is simple:
Entertain the user with the CSS and HTML of the page and let each script, one at a time, load and execute while the user handles the page. This has a disadvantage. While all that is happening, if the user tries to do something that requires scripting it's unknown whether it will work or not. In my case it's usually frustrating. Using defer in the head just below the CSS we can minimize that.
Using defer always decreases page loading time even if all scripts are cached when there's a need of, at least, one GET access to check if the script was changed (if-modified-since). The problem is that, for opera users (+-12% of the browser share), until opera implements defer, the page will seem to hang while the scripts are being loaded.

My tests say that placing the script tags all the way through the page is the best approach when having multiple .js files with defer and having in account that opera exists (it's the "balance" in action. Unfortunately that balance is hard to implement for phpbb3. So we are resumed to 3 options in which one is a variant of the other. IE will have external jQuery load synchronously.
1.1 Place all script tags in the head with defer (the best option if we lived in a great world where IE followed the current spec and Opera implemented defer)
1.2 Place the jQuery script tag in the head with defer and leave the rest of the tags where they are written in the document with defer (my favorite)
2. Place the jQuery and all the script tags at the bottom without forgetting to use the defer(the best option if defer didn't exist)

No matter which option it chosen, including defer is always the best option according to the tests' results.

So... What do you have to say?

The RFC: viewtopic.php?f=108&t=42497

[EDIT: Removed the scattered word. It can have a different meaning than the one I want to deliver. It's supposed to mean that the script tags are placed in the final code were they are written in the original code. Like what happens many times in the current stable phpbb version]
Last edited by brunoais on Sat Feb 25, 2012 9:43 am, edited 1 time in total.
brunoais
Registered User
 
Posts: 626
Joined: Fri Dec 18, 2009 3:55 pm

Re: To use defer or not to use defer

Postby Arty » Sat Feb 25, 2012 9:21 am

This is a good idea and I'd be all for it, but there are too many problems with almost no advantages.

There are too many things that can go wrong. Users customizing their styles do not know about differences in browser behavior, they will test it in one browser and will use that model, not knowing that other browsers behave differently. Most likely they'll check in WebKit or Mozilla browser, resulting in HTML5 spec. It will happen many many times.

Having IE conditional statements in code is ugly. I hate that.

What are advantages? Minor loading speed increase because document can be completely loaded before scripts are ran. That's all.

This advantage is not worth it. I think it shouldn't be implemented. Simply moving scripts to footer will is enough.

brunoais wrote:My tests say that placing the script tags scattered through the page is the best approach when having multiple .js files with defer and having in account that opera exists

That is the best approach for loading, but it is bad for customized styles. In my styles I often mess with DOM, moving whole blocks around to make it look correctly in old IE. If those blocks include scripts, those scripts are executed more than once when moving is done by copying html code. Because of that I already have moved all scripts from content to ether footer or header in my 3.0 styles. So in my opinion, having script tags scattered through out the page is very bad.
Formerly known as CyberAlien.
User avatar
Arty
Styles Team
Styles Team
 
Posts: 794
Joined: Wed Mar 06, 2002 2:36 pm
Location: Mars

Re: To use defer or not to use defer

Postby brunoais » Sat Feb 25, 2012 10:14 am

Arty wrote:This is a good idea and I'd be all for it, but there are too many problems with almost no advantages.

Hum... That's a point of view in what is the perfect approach. So if we change a but we get the best approach for us.
Arty wrote:There are too many things that can go wrong. Users customizing their styles do not know about differences in browser behavior, they will test it in one browser and will use that model, not knowing that other browsers behave differently. Most likely they'll check in WebKit or Mozilla browser, resulting in HTML5 spec. It will happen many many times.

We can create 2 very simple rules that will sole that all the way.
1st No inline scripts unless strictly necessary (I.e. communicate between php and javascript; to set js variables (examples)).
2nd You may not execute any code outside the $(document).ready(function (){/* here */}) or the $(window).load(function (){/* here */})

These are 2 very simple rules easy to test and apply. We could make them more complex to cover most possibilities and situations (E.g. functions outside the .ready() are ok but they must be called inside the .ready()) all but this will make sure that this problem will never happen.
Arty wrote:Having IE conditional statements in code is ugly. I hate that.

Hum... I'm also not a fan of them. We could do browser sniffing inside phpbb (good idea? Doesn't sound like it) or we could take the radical approach of just removing defer in the jQuery script tag. And apply to the rest.
Arty wrote:What are advantages? Minor loading speed increase because document can be completely loaded before scripts are ran. That's all.

I'm not used to call 2s (my college computer says it's 4s) in loading time minor. The objective is to let scripts download in parallel and run them in order. The spec mentions about executing the scripts only after the DOM is ready. I don't think that is necessary but that's the way it is done so we have to wait for the DOM to be ready for our scripts to be ran. Anyway, they are "just" ran after the DOM is ready, not downloaded and ran, downloaded, and ran, downloaded and ran, etc... Tests show that, if we remove the defer in the jQuery's script tag and keep in the others while keeping the script tags in order, nothing will fail.
Arty wrote:This advantage is not worth it. I think it shouldn't be implemented. Simply moving scripts to footer will is enough.

Man! I hate to see a message, try to delete it and instead of appearing the beautiful modal(?) window it changes the page to the earlier one. I have js turned on! [/counter_example_of_use_case]
Arty wrote:
brunoais wrote:My tests say that placing the script tags scattered through the page is the best approach when having multiple .js files with defer and having in account that opera exists

That is the best approach for loading, but it is bad for customized styles. In my styles I often mess with DOM, moving whole blocks around to make it look correctly in old IE. If those blocks include scripts, those scripts are executed more than once when moving is done by copying html code. Because of that I already have moved all scripts from content to ether footer or header in my 3.0 styles. So in my opinion, having script tags scattered through out the page is very bad.

[/quote]
Yep the best it's scattered like we have at the moment. But it's unpractical to maintain. So I think the top is good. But then we have the IE and Opera problem. So the bottom with defer seem to be the best option we can think of. Not as good as the browsers that do support correctly defer but, unfortunately, they will have to pay for the unbehaved ones. So I think we can make the best effort without the IE comments:


Move them all the way down there, apply defer to all <script> except the one to load jQuery and be happy.
brunoais
Registered User
 
Posts: 626
Joined: Fri Dec 18, 2009 3:55 pm

Re: To use defer or not to use defer

Postby Oleg » Sat Feb 25, 2012 9:34 pm

Replied in viewtopic.php?f=108&t=42497&p=235721#p235721. Can we keep defer debate to a single topic please?
Oleg
3.1 Release Manager
3.1 Release Manager
 
Posts: 1150
Joined: Tue Feb 23, 2010 2:38 am

Re: To use defer or not to use defer

Postby callumacrae » Sat Feb 25, 2012 9:50 pm

Oleg wrote:Can we keep defer debate to a single topic please?

+1 :-)
"In JavaScript, there is a beautiful, elegant, highly expressive language that is buried under a steaming pile of good intentions and blunders"
—Douglas Crockford

View my MOD, phpBB Mobile
User avatar
callumacrae
Website Team
Website Team
 
Posts: 882
Joined: Tue Apr 27, 2010 9:37 am
Location: England

Re: To use defer or not to use defer

Postby brunoais » Sun Feb 26, 2012 9:47 am

Please close this thread (but don't delete, at least, the 1st post)
I misunderstood the conversation I had in the IRC. Sorry about that. :oops:
brunoais
Registered User
 
Posts: 626
Joined: Fri Dec 18, 2009 3:55 pm

Re: To use defer or not to use defer

Postby imkingdavid » Sun Feb 26, 2012 5:38 pm

brunoais wrote:Please close this thread (but don't delete, at least, the 1st post)
I misunderstood the conversation I had in the IRC. Sorry about that. :oops:

Locked as per request. Please continue discussion here.
I do custom MODs. PM for a quote!
View My: MODs | Portfolio
Please do NOT contact for support via PM or email.
Remember, the enemy's gate is down.
User avatar
imkingdavid
Development Team
Development Team
 
Posts: 900
Joined: Thu Jul 30, 2009 12:06 pm


Return to [3.x] Discussion

Who is online

Users browsing this forum: No registered users and 17 guests