localStorage vs. Cookies

JavaScript performance comparison

Revision 19 of this test case created by Nicholas C. Zakas

Info

Tests the difference between operations with localStorage as compared to cookies.

Preparation code

<script>
  var value = document.cookie;

  if (value.length) {
      localStorage.setItem("foo", document.cookie);
  } else {
      value = "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef";
      localStorage.setItem("foo", value);
      document.cookie = value;
  }
 
  var value;
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Reading from cookie
value = document.cookie;
pending…
Reading from localStorage getItem
value = localStorage.getItem('foo');
pending…
Writing to cookie
document.cookie = "bar=fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo";
pending…
Writing to localStorage property
localStorage.setItem("bar", "fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo");
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:

6 comments

Jason Stoudt commented :

It would be interesting to learn if Safari's dominance here is due to the actual I/O operation happening asynchronously or the localStorage is buffered in memory. Or, maybe another method which I'm unable to conceive of.

Keith commented :

This is freezing chrome (17.0.963.66) for me each time on the cookie writing test. Wondering if none of the chrome results are accurate if that is happening to others.

Thomas commented :

Same here. Chrome 17 freezes.

Eric Wendelin commented :

Problem doesn't go away in Chrome 19... though if I wait a long time it gets past the cookie writing test and stalls on localStorage writing.

Bilal commented :

On Android Chome browser local storage out ran by ±1.31% faster and is the fastest

Add a comment