Jump to content

optimizing or not


bwakad
 Share

Recommended Posts

As I try to make code as short as possible, I would also like to have as less code as possible. The main thing for me is less resource heavy.

So I made 3 examples for a way to use my menu. Each have advantage and disadvantage. I would really like to know, which one is quicker. Because as my pages grow, so will the resources...

//  it is less likely the ID will change while names can be changed and the code stay the same

echo "<li><a href='{$pages->get(1026)->url}'>{$pages->get(1026)->name}</a></li>";

// more static, but when changing the page name, the code need to be rewritten

echo "<li><a href='{$config->urls->root}members/'>Browse Members</a></li>;

// automatic, although I'd probably need a IF for excluding menu items

$menu = $pages->find("template=browse, parent=1, sort=title");
                                   foreach ($menu as $item) { ?>
                                       <li><a href="<?php echo $item->url ;?>"><?php echo $item->title;?></a></li>

Link to comment
Share on other sites

Either it's the code (IF / FOREACH / INCLUDE) things, or the .js files (foundation/google)...

My page content loads quick, I just see in the top of my mozilla tab a spinner for at least a second.

<head> looks like this:

        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title><?php if($page->name == "profile") echo "{$user->name} "; echo $page->get("headline|title"); ?></title>
        <meta name="description" content="<?php echo $page->summary; ?>" />
        <meta name="generator" content="ProcessWire <?php echo $config->version; ?>" />
        <link rel="stylesheet" type="text/css" href="<?php echo $config->urls->templates?>mystyle/foundation.css" />
        <link rel="stylesheet" type="text/css" href="<?php echo $config->urls->templates?>mystyle/override.css" />
        <script type="text/javascript" src="<?php echo $config->urls->templates?>myscript/vendor/modernizr.js"></script>
        <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>

footer scripts are these:

            <script type="text/javascript" src="<?php echo $config->urls->templates?>myscript/vendor/jquery.js"></script>
            <script type="text/javascript" src="<?php echo $config->urls->templates?>myscript/foundation/foundation.stickyfooter.js"></script>
            <script type="text/javascript" src="<?php echo $config->urls->templates?>myscript/foundation/foundation.js"></script>
            <script type="text/javascript" src="<?php echo $config->urls->templates?>myscript/foundation/foundation.topbar.js"></script>
            <script type="text/javascript" src="<?php echo $config->urls->templates?>myscript/foundation/foundation.tooltip.js"></script>
            <script type="text/javascript" src="<?php echo $config->urls->templates?>myscript/foundation/foundation.abide.js"></script>
            <script type="text/javascript" src="<?php echo $config->urls->templates?>myscript/foundation/foundation.alert.js"></script>
            <script>
                  $(document).foundation();
            </script>

Link to comment
Share on other sites

Optimizing on this level would have minor benefits that it would hardly be noticable.

I would recommend limiting the amount of assets you load, the lesser the request to your server the faster the page will load.

Secondly to have a lot of speed aprovement i can recommend the ProCache module that Ryan wrote.

Altough i found a couple of things that you could change in your code:

echo "<li><a href='{$pages->get(1026)->url}'>{$pages->get(1026)->name}</a></li>";

// I would recommend changing it so you only have one request for $page->get()
$item = $pages->get(1026);
echo "<li><a href='{$item->url}'>{$item->name}</a></li>";
$menu = $pages->find("template=browse, parent=1, sort=title");
                                   foreach ($menu as $item) { ?>
                                       <li><a href="<?php echo $item->url ;?>"><?php echo $item->title;?></a></li>

// I would recommend changing it, not switch php html, and sort by name its significant faster that sorting by title
$menu = $pages->find("template=browse, parent=1, sort=name");
                                   foreach ($menu as $item) { 
                                       echo "<li><a href='{$item->url}'>{$item->title}</a></li>";

Regarding the amount of javascript files you load, you might want to compile those to one file and server that (possible minified and gzip compressed).

There are scripts or modules that can do that on the fly (sorry, i dont have an URL at this moment).

  • Like 3
Link to comment
Share on other sites

Regarding the amount of javascript files you load, you might want to compile those to one file and server that (possible minified and gzip compressed).

There are scripts or modules that can do that on the fly (sorry, i dont have an URL at this moment).

http://modules.processwire.com/modules/all-in-one-minify/

http://modules.processwire.com/modules/minify/

http://modules.processwire.com/modules/minify-html/

  • Like 2
Link to comment
Share on other sites

I think this thread fits my question as well

I´ve talked to david-karich and he told me that it´s probably better to have inline styles, at least for above-the-fold content

So I thought about having the whole stylesheet in the head like ages before, but get it included and minified to still have structured code for maintenance

Wrote a little function

function minCSS($styles) {
	foreach($styles as $style) {
		$buffer .= file_get_contents($style);
	}
	$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
	$buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer);
	$buffer = str_replace(': ', ':', $buffer);
	$buffer = str_replace(' {', '{', $buffer);
	$buffer = str_replace('{ ', '{', $buffer);
	$buffer = str_replace(';}', '}', $buffer);
	$buffer = str_replace('; ', ';', $buffer);
	$buffer = str_replace(', ', ',', $buffer);
	$buffer = str_replace(' + ', '+', $buffer);
	$buffer = str_replace(' > ', '>', $buffer);
	$buffer = str_replace("url('../", "url('/site/templates/", $buffer);
	
	$out = "<style type='text/css'>";
	$out .= $buffer;
	$out .= "</style>";
	return $out;
}

now I can

echo minCSS(array('styles/style1.css', 'styles/style2.css', 'styles/style3.css')); 

is it worth saving this request?

  • Like 1
Link to comment
Share on other sites

I do think optimising is not a bad thing at all, but I think it's a little bit of a trend. If the optimising is simple to do like compression With GZip do it.

If there's an automated process that caches the compressed results and servers the cached version back with proper headers go for it. 

Modern desktop browsers can load 6 assets parallel from the same url ( sub domains make an other 6 lanes free etc etc. ), and for mobile it's mostly 2. So bandwidth optimisation depends on platform. Then you have HTML CSS Javascript optimisation. If you break the paint process of the browser, the browser has to repaint the whole site. Javascript is a great behaviour changer of the dom, so if the js changes the positions, size etc etc, the whole dom has to be repainted.

The function you have above is a nice function for compression (saving a little on bandwidth), but it has to do it on every request. The expense here is for the server. It'l do fine if you don't have to many visitors I guess.

A minimise function should contain 3 basic things if you ask me:

1.) The minimizer

2.) Caching from the minimised.

3.) Send proper headers, while serving or not serving the cached results back.

But for little sites, with not much traffic a few kb more isn't a problem I think. In perspective, if you optimise 1 not optimised image you save way more KB's. I do think if you have 3 or 4 js files, concatenation isn't a big concern. Say if you have 7, 8 or more you could look at Davids minimiser.

  • Like 3
Link to comment
Share on other sites

Thanks Martijn!

Thought about caching too, but then I don´t save the request anymore. Or maybe I could use markup cache or just cache the whole template right?

I´m problaby totally wrong on this, but at least it was a little php exercise for me..digging more into functions and working with files a little ;-)

Link to comment
Share on other sites

I have found useful tips here for sure. But I also wonder if it helps when changing some basic things. For instance

- normal way - we access a page, include head.inc, include some other things, include foot.inc

- other way? - we have 1 page which holds head.inc and foot.inc, access a page and include it.

The way I see it,

in the normal way, especially with scripts and all, we have to load every time head.inc and foot.inc

in the other way, we load it once, and only load the page which we access

Although I do not really know what it will do with get/post requests....

Link to comment
Share on other sites

bwakad isn't the thing with the _init.php and _out.php like in the skyscrapers profile like what you mentioned?

think it's exactly what you are talking about and I like the way as well :-)

it doesn't really matter if the styles are producing a request or not when caching is enabled right? so no matter if I use my script or AIOM at the end..

Link to comment
Share on other sites

If we talk about requests we talk mainly about round trips. 

Browser receives HTML -> finds assets ( scripts styles images fonts etc. ) the browser ask a server to serve those -> the browser get them back.

Say you've 10 JS scripts, AIOM combines them to 1 saving your site from 9 round trips.

Thought about caching too, but then I don´t save the request anymore

This part I don't understand. 

If the code from your function is cached & served with proper headings, the next time the browser visits the file it'l tell the browser 304. Then the browser will get the file out of the browser cache. ( maybe only the header is send back, (not sure here), but never the whole file )

  • Like 1
Link to comment
Share on other sites

that´s what I meant with: 

it doesn't really matter if the styles are producing a request or not when caching is enabled right? so no matter if I use my script or AIOM at the end.. 

;)

  • Like 1
Link to comment
Share on other sites

In general:

I don't want to be rude as well, but many times it comes to that level (in answers) - it's always difficult to express yourself in written words.

True I do not know all basics from memory. And I appreciate all answers, especially the one that can learn me something. But I have had the same answer (go study) on a question about functions, although functions in PHP are not very basic. How valid is such an answer?

If we say 'studying some basic php' - and I add -> before we ask - it's basically telling me (and maybe others) 'not to ask any kind of questions'. But then again, who decides which question has to do with basic PHP?

Questions are never without a reason. You learn from the answers and tips when people trying to help. It is never of use to only one person. It can be of use to many.

And for the record:

People study PHP, even basics for years, and still ask questions of any kind, including mine. Since this topic is marked as a HOT topic I do not think it is a wrong question! Among us (also in this topic) are developers with advanced skills in PHP whom debate about it. I did not see anyone tell them to study basics!? I hope this has nothing to do with the time being a member or not being a contributor.

I merely ask questions when I do not understand or need some tips. The most easy thing to do is tell someone to study. The most difficult part is to put people on track.

  • Like 2
Link to comment
Share on other sites

I have been involved with computers and communications professionally since 1977 (that's a long time).  I say that to tell you that from what I have seen on this forum, the people contributing are doing it from the graciousness of their heart.  They are caring professionals who are making a living mostly as Developers or Designers.  They could simply let you wither and don't provide the information.  That's not how things are working on this forum.

I believe that if you truthfully went back over all of your forum postings, you would immediately see what some have rightfully mentioned to you.  There have been numerous posts where you are given the answers and then you go on to another tangent (not taking the valuable advice and working with it).

Everybody learns differently.  Some will grab onto knowledge faster than others.  None of the comments are meant to demean you.  The fact that they answered your posts and gave you valuable information shows that they actually care that you learn ProcessWire. You seem to want to do all of the hard stuff first, bypassing the low hanging fruits of ProcessWire.

You need to do your part.  One part is to take your own time and read some reference materials or take a PHP online course.  There are many free resources on the Internet that will provide the missing logic that is holding you back.  I believe you are very close to having that "Aha" moment, but you need to persevere, not lash out at people who are freely providing you with all the quality answers.

It's a testament to the quality of individuals on this forum that you have been fortunate to have gotten as much help so far.  We all want you to be a part of this community and thus we attempt to guide you in a different manner of learning.  You need to be able to take constructive criticism because that's a lesson anyone needs to know in the real working world.  Good day and good luck.

Charles 

Edited by cstevensjr
  • Like 10
Link to comment
Share on other sites

I merely ask questions when I do not understand or need some tips. The most easy thing to do is tell someone to study. The most difficult part is to put people on track.

Great point, I agree. Though, I don't think it's an invalid answer suggesting to maybe go back a few steps so that your current problem becomes more clear.

What I see in your situation is that you're trying to optimise in areas which might not be the source of the problem. I don't know your setup, but in most cases the frontend is the bottleneck.

Look at the profiler in chrome dev tools and find out if it's actually the site rendering (of the HTML via PHP, not the visual rendering) which is taking so long, or if it's the scripts, perhaps external sources and so on.

Link to comment
Share on other sites

Yes Charles, I understand what you say, but agree with me what I mentioned earlier:

"True I do not know all basics from memory. And I appreciate all answers, especially the one that can learn me something."

My question was about this code:

//  it is less likely the ID will change while names can be changed and the code stay the same
echo "<li><a href='{$pages->get(1026)->url}'>{$pages->get(1026)->name}</a></li>";

// more static, but when changing the page name, the code need to be rewritten
echo "<li><a href='{$config->urls->root}members/'>Browse Members</a></li>;

// automatic, although I'd probably need a IF for excluding menu items
$menu = $pages->find("template=browse, parent=1, sort=title");
                                   foreach ($menu as $item) { ?>
                                       <li><a href="<?php echo $item->url ;?>"><?php echo $item->title;?></a></li>

Were Nik provided me with some changes that make sense to me as I also mentioned some were above. If after that there is a remark like this:

"I don't want to be rude here @bwaked but the optimisation you need is studying some basic PHP." it does sound rude because it reads as if I myself need optimization.

It would be different if it was: "for optimization you need to study (this or that) more on php".

Probably my misconception of what was meant comes from my work which is law related. I used to read comma to comma, sentence to sentence, paragraph to paragraph. But as you read all my topics, most of them are friendly, useful for learning, and many times with a big thank you.

What I usually do is try a search on google and hopefully come to a website other then PW so not to bother you guys. But in many cases any link points to PW back. I can't help it if PW is becoming google's favorite :) . As such I made the topic in here.

All together I did had valuable feedback from Nik, the links to minify from someone else, and the mentioning about skyscraper's setup. But I think I better redraw myself from anymore questions.

Link to comment
Share on other sites

@bwaked: Your topic starts with about script optimising, quality answers are given. Then you start a whole other topic about assets loading. A good discussion comes out of this. Then out of the blue you switch to PHP include performance.

For PHP functions and stuff, there's google to find valid answers to your PHP questions. I'm sorry, we are not Stack Overflow. We are the ProcessWire forum. We as developer all have to learn. We all have to search or test for the answers we need. It's just to easy to ask all questions and letting other people answer/work for you.

Then you're not letting me know that you're picking up these answers, there's not much response. Lack of PHP knowledge on your side, makes it difficult for you to have a clear overview I guess. But PHP can be learned by study. That your part.

And then there is the language thing as we both speak better Dutch then English.
  • Like 4
Link to comment
Share on other sites

Well, I guess you speak for the whole forum. It's true - we speak the same, but write different. btw: The assets loading was in response to your question. And the include still have to do with optimizing code. So in short they all fall within the same topic I guess and therefore would not be good to start yet another topic. But maybe we should have a guideline -

what is allowed to ask;

in what forum;

when to ask;

and by whom.

I did not and never will ask anyone for an entire site to be rewritten for me. You already mentioned such thing before "letting others do the work for me" but I do not think this is fair to say. I never, not once, received code for a whole page which I could just copy and paste. I did however pick up on some of (your) tips regarding selector and template. From which I think is valuable information you gave me. If you could look at my code none of it would resemble any of yours or others.

But all of this is really off-topic now. So, let me thank you for your answer. No hard feelings on my part.

Link to comment
Share on other sites

All together I did had valuable feedback from Nik

@bwaked:Respect to Nik, but I think the most valuable advise here is from cstevensjr.

I also don't wish to sound rude so please don't take this as so;

When other users take the time to reply to your questions you need to do likewise - take the time to study their responses before plunging onward. I have been guilty of the same myself at times, but have bben around this community a little longer than you and have come to realise there is an immense amount of knowledge among members -  more-so than most communities - and usually their answers are spot-on so long as the question is clear and precise. Sometimes it just takes a little extra study of their responses to understand them.

You will get a much more positive response from members if you treat their responses like the gold dust they usually are :) 

Onward bro....

  • Like 5
Link to comment
Share on other sites

@bwakad, just one more thing: if you feel that asking questions is the best for you, then ask. No one here is annoyed by them, or we would just ignore. As you must have noticed, people here like to answer to questions, even if they are basic or don't even seem to make sense. When someone points it out to you that you shouldn't be making a specific question, it's not because they don't want to answer, but because that question shows that you didn't look for things the right way. When Martijn points out that you need some basic knowledge of PHP before asking about site optimisation, it's because you are clearly inverting the priorities on your learning process, not to criticise. Sometimes it feels like your learning process is a bit like the sentence you have on your profile :P

  • Like 5
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...