localStorage getObject vs global variable

JavaScript performance comparison

Test case created by Josh Vermaire

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var lS = {
      setObject: function(key, value) {
        localStorage.setItem(key, JSON.stringify(value));
      },
      getObject: function(key) {
        var value = localStorage.getItem(key);
        return value && JSON.parse(value);
      }
    }
   
    //simple boolean in local storage
    lS.setObject('bool', true);
    //simple object in local storage
    lS.setObject('object', { attribute: 'someAttribute' })
    //simple boolean
    lS.bool = true
    //simple object
    lS.object = { attribute: 'someAttribute' }
};
</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
Read boolean from localStorage
lS.getObject('bool');
pending…
Read object from localStorage
lS.getObject('object');
pending…
Read boolean
lS.bool;
pending…
Read object
lS.object;
pending…

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

Compare results of other browsers

0 comments

Add a comment