Forum Moderators: phranque

Message Too Old, No Replies

Which is better: load section via Ajax or PHP include()

         

csdude55

5:53 am on Mar 17, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm approaching this from a performance and usability point of view. I've found that every fraction of a second I can shave off of the load time results in more pages per session, so the main goal is for the page to load faster for the end user, and be usable faster.

I have several scripts that are currently like:

<?php

echo <<<EOF
<div id="example">

EOF;

include 'example.php';

echo <<<EOF
</div>
<script>
setInterval("$('#example').ajax('example.php')", 100000);
</script>

EOF;

?>


Please forgive any typos, that's just written for the example. But the concept is that I include example.php via PHP, then update it every so often via the setInterval...ajax script.

What I'm wondering is, would it be faster, or better for the end-user, if non-essential things that were loaded like this were not included via PHP at all, but just loaded using the Ajax:

<?php

echo <<<EOF
<div id="example"></div>
<script>
// not 100% sure whether I should wrap this in $(function()... ?
$(function() {
$('#example').ajax('example.php');
});

setInterval("$('#example').ajax('example.php')", 100000);
</script>

EOF;

?>


I know that THESE sections wouldn't load until the rest of the page is loaded (if I understand correction, $(function()... means that they won't begin to load until the document is complete?), and they would rely on the user to have Javascript enabled, but if they're not essential to the page then would that matter?

(Note, by "non essential", I'm referring to things like, using this site as an example, the "You have mail" script at the top of the page, or the entire right column... useful, but not critical to use the site.)

My theory is that the rest of the page would load faster, and thus be usable more quickly. Or am I wrong, and everything would be at a standstill until those sections loaded, resulting in a longer time before it's usable?

keyplyr

6:12 am on Mar 17, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Just s FYI - If you use includes, I would choose SSI (server side includes, also referred to as virtual includes) instead of PHP includes.

graeme_p

7:09 am on Mar 17, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I doubt it would have it meaningfully faster unless the HTML you are removing from the initial page load is very heavy - you are not saving a request, just a small chunk of HTML. Not worth it given you will add an extra request to every page load.

csdude55

11:17 pm on Mar 17, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I guess "very heavy" is subjective. The parts I've been playing include one section that loads a Google news XML feed (with 3 remote images), a section that loads a second remote news XML feed, a section that loads a remote weather feed (1 local image), a section that builds a local calendar (about 200 lines of HTML output), then 3 other sections that load data from a local MySQL db (and each include 1 local image).

Every 15 minutes, I use the sections that load remote XML feeds to make a new copy locally (just saving the XML to a data file). So 1 person every 15 minutes gets hit with a little more of a delay, but then everyone else is reading it from the local file.

Otherwise, I'm pushing the load of 3 remote images and 4 local images to the separate load, along with about 1500 lines of HTML code (from 2797 to 1315 lines).

It's also worth noting that, in the View Source, I'm moving the initial <H1> line from line 1283 to line 320.

A simple speed test (webpagetest.org) shows that the new style is slower, but I'm not certain whether that "slower" translates to the page being unusable for a longer time, or if it's more usable quicker while waiting on those other parts to load separately.

tangor

11:57 pm on Mar 17, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Anything with remote content is going to slow the page down. To test it, comment out all the parts that are remote, test your pages for speed, then add in, one at a time test and then remove, each of the remote parts to find out which is the slowest. That's the part you then leave out if you are really looking to speed up your page service to the end user.

lucy24

12:34 am on Mar 18, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you use includes, I would choose SSI (server side includes, also referred to as virtual includes) instead of PHP includes.

If the pages are already PHP, will it make any difference?

keyplyr

12:44 am on Mar 18, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If the pages are already PHP, will it make any difference?
If the pages are PHP or HTML pages already being parsed with the PHP engine, best to keep includes PHP. Otherwise, if PHP is not being used globally, it's more efficient to use SSI, at least on server configs I've used.

csdude55

1:10 am on Mar 18, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That's the part you then leave out if you are really looking to speed up your page service to the end user.


By "leave out", do you mean to load it via Ajax instead of includes or SSI? I know that Google news is relatively slow, but it does add value to the site (and encourages discussion) so I don't want to just cut it out entirely. In fact, if loading via Ajax makes the rest of the site usable while the Ajax section is loading, I'm hoping to include these sections on more pages throughout the site.

graeme_p

5:10 am on Mar 22, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You could poll the remote content every 15 minutes and cache it without it being hit by a user.

csdude55

5:35 am on Mar 22, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think my situation is a little unique, Graeme. I have 51 domains parked on top of the main domain, and I load content based on the domain that's being used. Each one of those domains call a different remote news feed and weather feed.

Some of the domains are super busy, and 100 pageviews a second wouldn't be uncommon. So those, yes, I could set up a cron with no problem.

But others are pretty slow, and might only have a few visitors a day. So for them, a cron that cached the feed unnecessarily every 15 minutes would be unnecessary.

Plus, with 51 parked domains, that would be 102 different crons set up (plus I would have to manually add a new cron each time I added a new domain). It would be difficult to stagger them so that several didn't run consistently at the same time.

That's why I set this system up originally; I didn't want to cause an unnecessary server load on my end, or on the remote XML server's end.

But you're right, if pushing everything to Ajax slows things down then I COULD do a cron. It's just a little more work.

I'm mainly curious now, though, whether reducing the HTML output, and decreasing the HTML before you get to the "main" content (moving from line 1283 to line 320) would have an impact on search engines?