0
\$\begingroup\$

There is an e-commerce site that I'm working on that has thousands of pages in it (mostly product pages). I've got some jQuery that I'm running on about 1300 of these products. Only specific products need the code to run so what I've done is set up the product codes (which display in the URL of the product) in a JSON data sheet and I'm looping through the JSON data like this:

$(document).ready(function(){

$.getJSON('js/round.js', function(data) {

    var location_var = "location.pathname.indexOf('/product-p/main-product.htm')"

    for(i=0; i<data.records.length-1; i++){     
        var location_var = location_var + " || location.pathname.indexOf('" + data.records[i].productcode + "')";
    }

    if (location_var != -1){
        //DO SOMETHING TO THESE SPECIFIC PAGES
    }

});         
});

Now this functions but obviously it has to handle the 1300 hundred rows, check and then run my code. This causes a slight lag and unfortunately that lag will run on all of the pages because it has to determine if the page is correct for every page that it goes through.

This is the only way I could think of doing this (my Javascript is a bit limited at this point) so I'm curious if anybody knows of a way to do this more efficiently and to make the query faster.

Thanks for your help!

\$\endgroup\$
  • 2
    \$\begingroup\$ Your code makes no sense. You are just concatenating strings together, location_var will never equal -1. Please provide a better explanation of what you are trying to achieve and what is data. \$\endgroup\$ – plalx Oct 14 '13 at 6:03
  • \$\begingroup\$ -1 is used when you are targeting a URL. It's very common when you are tried to run code on a specific page. Data is the response returned when running the getJSON function. It makes plenty of sense. \$\endgroup\$ – MxmastaMills Oct 14 '13 at 19:20
  • \$\begingroup\$ What do you expect the type of location_var will be? Do you think you can compare it to the typeof -1 ? \$\endgroup\$ – Max Nov 13 '13 at 14:09
1
\$\begingroup\$

What exactly do you have in the javascript (json) file? Can you get the product code from anywhere other than the the path?

Here is what I suggest:

  1. Just include the javascript file. Don't bother pulling it via JSON. Doing so just adds overhead you don't need since you are checking this file on every page anyway.

  2. Within the javascript file store the product codes in an array.

    var productCodes = ['code1', 'code2', 'etc...']
    
  3. Put the test in a separate function.

    function pageNeedsProcessing() {
        // add any special rules here to avoid checks when not needed.
        if (location.pathname.indexOf('/product-p/main-product.htm')) {
            return true;
        }
    
        // do whatever you need to get the product code.  This may require parsing the path.
        var prodCode = ???;
    
        // if the product code is in array (established in #2) then return true.
        return $.inArray(prodCode, productCodes)  
    }
    
  4. Add your code to be processed.

    if (pageNeedsProcessing()) {
          //DO SOMETHING TO THESE SPECIFIC PAGES
    }
    

One caveat here is that doing it exactly like this will introduce some variables into global scope. But there are ways around that.

\$\endgroup\$
0
\$\begingroup\$

In general,

the native [].forEach() will work faster than your own iteration, at the very least you ought to pre-calculate the length of data.records.

You should also profile your JavaScript using for example the Chrome Developer tools so that you can find out where most time is spent. I kind of doubt it would be indexOf. Can you provide the actual code for //Do something to these specific pages ?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.