Martin Borthiry
Martin Borthiry

Reputation: 5326

Faster and shorter way to check if a cookie exists

What is the shorter and faster way to know if a cookie has a value or exists?

I'm using this to know if exists:

 document.cookie.indexOf('COOKIENAME=')== -1

This to know if has a value

 document.cookie.indexOf('COOKIENAME=VALUE')== -1

Any better? Any problems on this method?

Upvotes: 5

Views: 23785

Answers (3)

Moritz Roessler
Moritz Roessler

Reputation: 8621

I would suggest writing a little helper function to avoid what zzzzBov mentioned in the comment

  • The way you use indexOf, it would only evaluate correct if you check for the containment of a String in a cookie, it doesn't match a complete name, in that case the above would return false therefore giving you the wrong result.

function getCookie (name,value) {
    if(document.cookie.indexOf(name) == 0) //Match without a ';' if its the firs
        return -1<document.cookie.indexOf(value?name+"="+value+";":name+"=")
    else if(value && document.cookie.indexOf("; "+name+"="+value) + name.length + value.length + 3== document.cookie.length) //match without an ending ';' if its the last
        return true
    else { //match cookies in the middle with 2 ';' if you want to check for a value
        return -1<document.cookie.indexOf("; "+(value?name+"="+value + ";":name+"="))
    }
}
getCookie("utmz") //false
getCookie("__utmz" ) //true

However, this seems to be a bit slow, so giving it an other approach with splitting them Those are two other possibilities

function getCookie2 (name,value) {
    var found = false;
    document.cookie.split(";").forEach(function(e) {
        var cookie = e.split("=");
        if(name == cookie[0].trim() && (!value || value == cookie[1].trim())) {
            found = true;
        }
    })
    return found;
}

This one, using the native forEach loop and splitting the cookie array

function getCookie3 (name,value) {
    var found = false;
    var cookies = document.cookie.split(";");
    for (var i = 0,ilen = cookies.length;i<ilen;i++) {
         var cookie = cookies[i].split("=");
         if(name == cookie[0].trim() && (!value || value == cookie[1].trim())) {
              return found=true;
         }
     }
     return found;
};

And this, using an old for loop, which has the advantage of being able to early return the for loop if a cookie is found

Taking a look on JSPerf the last 2 aren't even that slow and only return true if theres really a cookie with the name or value, respectively

I hope you understand what i mean

Upvotes: 4

trante
trante

Reputation: 34006

I use Jquery cookie plugin for this.

<script type="text/javascript" src="jquery.cookie.js"></script>

function isCookieExists(cookiename) {
    return (typeof $.cookie(cookiename) !== "undefined");
}

Upvotes: 0

Cerbrus
Cerbrus

Reputation: 72857

Apparently:

document.cookie.indexOf("COOKIENAME=VALUE");

For me, is faster, but only slightly.

As the test shows, surprisingly, it's even faster to split the cookie up into arrays first:

document.cookie.split(";").indexOf("COOKIENAME=VALUE");

Upvotes: 4

Related Questions