localStorage vs. Objects
JavaScript performance comparison
Info
Tests the difference between reading from localStorage as compared to reading data from an object.
Preparation code
<script>
localStorage.setItem("foo", "body { background: blue; margin: 10px; padding: 10px; color: black; text-decoration: none; }");
var FOO = {
foo: "body { background: blue; margin: 10px; padding: 10px; color: black; text-decoration: none; }"
};
var value;
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Reading from an object |
|
pending… |
Reading from localStorage |
|
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:
- Revision 1: published by Nicholas C. Zakas
- Revision 2: published
- Revision 3: published
- Revision 4: published by Morgan Cheng
- Revision 6: published by Ben Voran
- Revision 7: published by Ben Voran
- Revision 8: published by Ben Voran
- Revision 10: published by Bram Stein
- Revision 11: published by jurby
- Revision 13: published
- Revision 14: published
- Revision 16: published by Asim
- Revision 17: published
- Revision 19: published by Nicholas C. Zakas
- Revision 20: published by Sebastiaan
- Revision 21: published
8 comments
Considering the dominance of iOS devices, I wonder how this does on desktops with SSDs. Do browsers not have a localStorage in-memory cache?
So, access to local storage should be treated the same way as access to
.propertyvalues that are referenced repeatedly – create a locally scoped var, store the value in the var, and only reference the var repeatedly.I edited the tests to test that theory, but then realized, I have no idea what the scope of the code snippets is… http://jsperf.com/localstorage-vs-objects/3
Chrome's numbers are bizarre! Their object reads are by far the fastest of any browser, but their localStorage numbers are the slowest by an even greater (much greater margin). They're about 40x slower than an iPhone!
Isn't this obvious? I'm pretty sure that
localStoragerequires spinning up the hard drive. Reading from an object in memory should be way faster. The tests showed that.When reading from
localStorageyou're using a function, whereas when reading from an object you use direct property access. I wonder if the results were slightly different, if you called a function like that:SSDs don't have any impact:
Same here. SSDs don't have any impact:
Firefox 4.0.1 on Linux x86_64 Reading from an object 66,371,629
Reading from localStorage 45,321
You’re comparing
FOO.footolocalStorage.getItem('foo')here. For a fair test you should probably skip the function call, and uselocalStorage.foo(which works just as well in this case).Update: Someone made a revision with these changes: http://jsperf.com/localstorage-vs-objects/10