Join Free
+ Reply to Thread
Results 1 to 6 of 6
  1. #1

    Code optimization tips

    I'm going to start a list of sites and docs about code optimization. Please feel free to contribute anything you come across:

  2. #2
    This is a tip that I read about and used today for a site that I'm working on. It's not a PHP tip, but a JS tip.

    I like to use jQuery and jQueryUI because they make it really easy to do cool things on your site in not a lot of time. The trouble is that they're huge, even with gzip. So, defer them until the page is loaded. Let this be the only JS that runs on your site in real time. It will call jQuery and any other JS that you need once the page is loaded. That way your web visitors are looking at banners while you load your JS libraries

    I didn't write this, I grabbed it from stackoverflow:

    var deferredJSFiles = ['http://path/to/jquery.js', 'http://path/to/jqueryUI.js', 'localJSfile1.js', 'localJSfile2.js'];
    function downloadJSAtOnload() {
        if (!deferredJSFiles.length) return;
        var deferredJSFile = deferredJSFiles.shift();
        var element = document.createElement('script');
        element.src = deferredJSFile;
        element.onload = element.onreadystatechange = function() { if (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete') downloadJSAtOnload(); };
    if (window.addEventListener) window.addEventListener('load', downloadJSAtOnload, false);
    else if (window.attachEvent) window.attachEvent('onload', downloadJSAtOnload);
    else window.onload = downloadJSAtOnload;

  3. #3
    Good point. You can also use mod_pagespeed from google. It has an optimization that rewrites your url's so jquery is included from google's servers instead of yours. They do this because chances are, the visitors browser already has the google version cached, so it doesn't have to load again on your site.

    Another JS tip that Andrew recently performed. We used jquery in our performance ads, but as you pointed out, it's huge! Instead, he wrote everything out in native js. A bit less convenient for you as a programmer, but his code is now 100X smaller.

  4. #4
    haha yeah, if you're only likely using a tiny percent of the jQuery library... if I wasn't so lazy I would do the same I'm so happy to let jQuery worry about all the cross browser crap though

  5. #5
    Oh I know, we spend a lot of time dealing with cross browser issues. For us it makes sense because we have to handle billions of requests. For an average shopping site you would never notice the difference i'm sure.

  6. #6
    I would definitely agree with using jQuery for most projects. It didn't make sense on Performance Ads because it was blocking the rest of your content from loading while it downloads the library. The last thing we want is our ads slowing down your site.

    This brings up a good point. A lot of us include multiple third-party js scripts on our sites. Be wary of what you are including. Don't let someone else's poorly written code affect the performance of the site that your are working so hard to optimize.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
coupons | coupons and deals