jquery.cookie

JavaScript performance comparison

Revision 4 of this test case created

Info

Test performance of jquery.cookie access

Preparation code

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.3.1/jquery.cookie.min.js"></script>
<script>
Benchmark.prototype.setup = function() {
    $.cookie("TEST", 123);
    $.cookie("TEST2", 345);
    $.cookie("TEST3", 123);
    $.cookie("TEST4", 345);
    $.cookie("TEST5", 123);
    $.cookie("TEST6", 345);
   
    var getCookieValue = function(key) {
      return $.cookie(key);
    }
   
    var cookieHash = {};
    var currentCookieVal = undefined;
    var isCacheValid = function() {
      return currentCookieVal == document.cookie;
    }
    var getHashedCookieVal = function(key) {
      if (!isCacheValid()) {
        //console.log('Cached cookie was invalid!');
        currentCookieVal = document.cookie;
        cookieHash = {};
        cookieHash[key] = $.cookie(key);
      }
      else if (!cookieHash[key]) {
        cookieHash[key] = $.cookie(key);
      }
      //else {
        //console.log('Using cached cookie value');
      //}
      return cookieHash[key];
    }
   
    var cookieHash2 = {};
    var getHashedCookieValNoCacheCheck = function(key) {
      if (!cookieHash[key]) {
        cookieHash[key] = $.cookie(key);
      }
      return cookieHash[key];
    }
   
    var getRawCookie = function() {
      return document.cookie;
    }
   
};

Benchmark.prototype.teardown = function() {
    $.cookie("TEST", null);
    $.cookie("TEST2", null);
    $.cookie("TEST3", null);
    $.cookie("TEST4", null);
    $.cookie("TEST5", null);
    $.cookie("TEST6", null);
   
};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Accessing Cookie via $.cookie
for (var i=0; i<100; i++) {
  var value = getCookieValue("TEST");
}
pending…
Accessing Cookie via object hash with cache check
for (var i=0; i<100; i++) {
  var value = getHashedCookieVal("TEST");
}
pending…
Accessing Cookie via object hash with no cache check
for (var i=0; i<100; i++) {
  var value = getHashedCookieValNoCacheCheck("TEST");
}
pending…
Get raw cookie
for (var i=0; i<100; i++) {
  var value = getRawCookie();
}
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment