More Efficient Javascript indexOf query 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&lt;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!
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:


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.
Within the javascript file store the product codes in an array.  

var productCodes = ['code1', 'code2', 'etc...']

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)  
}

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.