JSON stringify or string for localStorage

JavaScript performance comparison

Test case created by Pamela Fox

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
Always use JSON.stringify
function store(key, val) {
  localStorage.setItem(key, JSON.stringify(val));
}

store('num', 1);
store('on', true);
store('name', 'pamela');
store('obj', {'hello': 'world'});
pending…
Optionally with param
function store(key, val, isObject) {
  if (isObject) {
    localStorage.setItem(key, JSON.stringify(val));
  } else {
    localStorage.setItem(key, val);
  }
}

store('num', 1);
store('on', true);
store('name', 'pamela');
store('obj', {'hello': 'world'}, true);
pending…
Optionally with no string csting
function store(key, val, isObject) {
  if (isObject) {
    localStorage.setItem(key, JSON.stringify(val));
  } else {
    localStorage.setItem(key, val);
  }
}

store('num', '1');
store('on', 'true');
store('name', 'pamela');
store('obj', {'hello': 'world'}, true);
pending…
Optionally with typeof
function store(key, val) {
  if (typeof val === 'object') {
    localStorage.setItem(key, JSON.stringify(val));
  } else {
    localStorage.setItem(key, val);
  }
}

store('num', 1);
store('on', true);
store('name', 'pamela');
store('obj', {'hello': 'world'});
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.

1 Comment

Alex Schleber commented :

How is it that Safari is at least about 50x faster than Chrome on this?! (~400k vs. 8k ops) (yes, I'm on Mac OS X 10.6.8)

Does Apple hamstring Chrome, or what? Do I first need to close all other Chrome tabs?