localStorage vs. Cookies

JavaScript performance comparison

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

Preparation code

  var value = document.cookie;

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

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Reading from cookie
value = document.cookie;
Reading from localStorage getItem
value = localStorage.getItem('foo');
Writing to cookie
document.cookie = "bar=fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo";
Writing to localStorage property
localStorage.setItem("bar", "fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo");

Compare results of other browsers


You can edit these tests or add even more tests to this page by appending /edit to the URL.


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