a brief introduction to local storage

105
A Brief Introduction to localStorage Shouqiang Gong - @StuPig_me 13719星期五

Upload: shouqiang-gong

Post on 17-May-2015

606 views

Category:

Business


0 download

TRANSCRIPT

Page 1: A Brief Introduction To Local Storage

A Brief Introduction to localStorage

Shouqiang Gong - @StuPig_me

13年7月19⽇日星期五

Page 3: A Brief Introduction To Local Storage

APIMETHOD ARGS RETURNS

setItem string key, string value

getItem string key string value

removeItem string key

clear

key int index string key

length int length

Read more: localStorage spec

13年7月19⽇日星期五

Page 4: A Brief Introduction To Local Storage

APIMETHOD ARGS RETURNS

setItem string key, string value

getItem string key string value

removeItem string key

clear

key int index string key

length int length

Read more: localStorage spec

13年7月19⽇日星期五

Page 5: A Brief Introduction To Local Storage

APIMETHOD ARGS RETURNS

setItem string key, string value

getItem string key string value

removeItem string key

clear

key int index string key

length int length

Read more: localStorage spec

13年7月19⽇日星期五

Page 6: A Brief Introduction To Local Storage

APIMETHOD ARGS RETURNS

setItem string key, string value

getItem string key string value

removeItem string key

clear

key int index string key

length int length

Read more: localStorage spec

13年7月19⽇日星期五

Page 7: A Brief Introduction To Local Storage

APIMETHOD ARGS RETURNS

setItem string key, string value

getItem string key string value

removeItem string key

clear

key int index string key

length int length

Read more: localStorage spec

13年7月19⽇日星期五

Page 8: A Brief Introduction To Local Storage

APIMETHOD ARGS RETURNS

setItem string key, string value

getItem string key string value

removeItem string key

clear

key int index string key

length int length

Read more: localStorage spec

13年7月19⽇日星期五

Page 9: A Brief Introduction To Local Storage

APIMETHOD ARGS RETURNS

setItem string key, string value

getItem string key string value

removeItem string key

clear

key int index string key

length int length

Read more: localStorage spec

13年7月19⽇日星期五

Page 10: A Brief Introduction To Local Storage

Snippet

13年7月19⽇日星期五

Page 11: A Brief Introduction To Local Storage

Snippet

// original API

localStorage.setItem('decade', '00s');

localStorage.setItem('song', JSON.stringify({artist: 'ColdPlay', title: 'Yellow'}));

var decade = localStorage.getItem('decade');var song = JSON.parse(localStorage.getItem('song'));

alert('Best song in the ' + decade + ' was ' + song.title + ' by ' + song.artist);

13年7月19⽇日星期五

Page 12: A Brief Introduction To Local Storage

Store.js

other libraies: locache, LawnChair

13年7月19⽇日星期五

Page 13: A Brief Introduction To Local Storage

Store.js

// 3rd library - store.js// https://github.com/marcuswestin/store.js

store.set('decade', '00s')store.set('song', {artist: 'ColdPlay', title: 'Yellow'});

var decade = store.get('decade');var song = store.get('song');

alert('Best song in the ' + decade + ' was ' + song.title + ' by ' + song.artist);

other libraies: locache, LawnChair

13年7月19⽇日星期五

Page 14: A Brief Introduction To Local Storage

Event

Read more: storage event spec

13年7月19⽇日星期五

Page 15: A Brief Introduction To Local Storage

Event

PROPERTY TYPE

key string

oldValue any

newValue any

url string

*timestamp double

Read more: storage event spec

13年7月19⽇日星期五

Page 16: A Brief Introduction To Local Storage

Event

PROPERTY TYPE

key string

oldValue any

newValue any

url string

*timestamp double

Read more: storage event spec

13年7月19⽇日星期五

Page 17: A Brief Introduction To Local Storage

Event

PROPERTY TYPE

key string

oldValue any

newValue any

url string

*timestamp double

Read more: storage event spec

13年7月19⽇日星期五

Page 18: A Brief Introduction To Local Storage

Event

PROPERTY TYPE

key string

oldValue any

newValue any

url string

*timestamp double

Read more: storage event spec

13年7月19⽇日星期五

Page 19: A Brief Introduction To Local Storage

Event

PROPERTY TYPE

key string

oldValue any

newValue any

url string

*timestamp double

Read more: storage event spec

13年7月19⽇日星期五

Page 20: A Brief Introduction To Local Storage

Event

PROPERTY TYPE

key string

oldValue any

newValue any

url string

*timestamp double

Read more: storage event spec

13年7月19⽇日星期五

Page 22: A Brief Introduction To Local Storage

StorageEvent// StorageEvent doesn’t bubble and is not cancelable

window.addEventListener('storage', function (e) { if (!e) e = window.event;}, false)

// trigger the event manuallyvar evt = document.createEvent('StorageEvent');// args: type canBubble cancelBubble key oldVaule // newValue url storageAreaevt.initStorageEvent('StorageEvent', ...);window.dispatch(evt);

other libraies: StorageEvent

13年7月19⽇日星期五

Page 23: A Brief Introduction To Local Storage

Compatibility

13年7月19⽇日星期五

Page 24: A Brief Introduction To Local Storage

Compatibilityworks well in major browsers...

13年7月19⽇日星期五

Page 25: A Brief Introduction To Local Storage

Compatibilityworks well in major browsers...

13年7月19⽇日星期五

Page 26: A Brief Introduction To Local Storage

Capacity

Read more: localStorage capacity

13年7月19⽇日星期五

Page 27: A Brief Introduction To Local Storage

Capacityhas a limited capacity (2-5MB) per subdomain...

Read more: localStorage capacity

13年7月19⽇日星期五

Page 28: A Brief Introduction To Local Storage

Capacityhas a limited capacity (2-5MB) per subdomain...

Browser localStorage sessionStorage globalStorage

Android2.2 - 4.2 2.49M unlimited

(except 3.0) none

iPhone3.1 - 6.1 2.49M 2.49M none

Chrome Mobile16 - 28 2.49M 2.49M none

IE Mobile9 - 10 4.75M 4.75M none

Read more: localStorage capacity

13年7月19⽇日星期五

Page 29: A Brief Introduction To Local Storage

Capacityhas a limited capacity (2-5MB) per subdomain...

Browser localStorage sessionStorage globalStorage

Android2.2 - 4.2 2.49M unlimited

(except 3.0) none

iPhone3.1 - 6.1 2.49M 2.49M none

Chrome Mobile16 - 28 2.49M 2.49M none

IE Mobile9 - 10 4.75M 4.75M none

Read more: localStorage capacity

13年7月19⽇日星期五

Page 30: A Brief Introduction To Local Storage

Capacityhas a limited capacity (2-5MB) per subdomain...

Browser localStorage sessionStorage globalStorage

Android2.2 - 4.2 2.49M unlimited

(except 3.0) none

iPhone3.1 - 6.1 2.49M 2.49M none

Chrome Mobile16 - 28 2.49M 2.49M none

IE Mobile9 - 10 4.75M 4.75M none

Read more: localStorage capacity

13年7月19⽇日星期五

Page 31: A Brief Introduction To Local Storage

Capacityhas a limited capacity (2-5MB) per subdomain...

Browser localStorage sessionStorage globalStorage

Android2.2 - 4.2 2.49M unlimited

(except 3.0) none

iPhone3.1 - 6.1 2.49M 2.49M none

Chrome Mobile16 - 28 2.49M 2.49M none

IE Mobile9 - 10 4.75M 4.75M none

Read more: localStorage capacity

13年7月19⽇日星期五

Page 32: A Brief Introduction To Local Storage

Capacityhas a limited capacity (2-5MB) per subdomain...

Browser localStorage sessionStorage globalStorage

Android2.2 - 4.2 2.49M unlimited

(except 3.0) none

iPhone3.1 - 6.1 2.49M 2.49M none

Chrome Mobile16 - 28 2.49M 2.49M none

IE Mobile9 - 10 4.75M 4.75M none

Read more: localStorage capacity

13年7月19⽇日星期五

Page 33: A Brief Introduction To Local Storage

Scope

13年7月19⽇日星期五

Page 34: A Brief Introduction To Local Storage

Scope• is scoped to the subdomain

13年7月19⽇日星期五

Page 35: A Brief Introduction To Local Storage

Scope• is scoped to the subdomain

• values on Secure (SSL) pages are isolated

13年7月19⽇日星期五

Page 36: A Brief Introduction To Local Storage

Scope• is scoped to the subdomain

• values on Secure (SSL) pages are isolated

• sessionStorage values survive some browser restarts

13年7月19⽇日星期五

Page 37: A Brief Introduction To Local Storage

Scope• is scoped to the subdomain

• values on Secure (SSL) pages are isolated

• sessionStorage values survive some browser restarts

• values created in “incognito” mode are isolated

13年7月19⽇日星期五

Page 38: A Brief Introduction To Local Storage

Usage

13年7月19⽇日星期五

Page 39: A Brief Introduction To Local Storage

Usage

• persistent user data

13年7月19⽇日星期五

Page 40: A Brief Introduction To Local Storage

Usage

• persistent user data

• persistent app state

13年7月19⽇日星期五

Page 41: A Brief Introduction To Local Storage

Usage

• persistent user data

• persistent app state

• filled-in forms

13年7月19⽇日星期五

Page 42: A Brief Introduction To Local Storage

Usage

• persistent user data

• persistent app state

• filled-in forms

• client cache

13年7月19⽇日星期五

Page 43: A Brief Introduction To Local Storage

Persistent User Data

try: Dillinger

13年7月19⽇日星期五

Page 44: A Brief Introduction To Local Storage

Persistent User Datasave user profile...

try: Dillinger

13年7月19⽇日星期五

Page 45: A Brief Introduction To Local Storage

Persistent User Datasave user profile...

try: Dillinger

13年7月19⽇日星期五

Page 46: A Brief Introduction To Local Storage

Persistent App State

try: JSHint

13年7月19⽇日星期五

Page 47: A Brief Introduction To Local Storage

Persistent App Stateremember checked options...

try: JSHint

13年7月19⽇日星期五

Page 48: A Brief Introduction To Local Storage

Persistent App Stateremember checked options...

try: JSHint

13年7月19⽇日星期五

Page 50: A Brief Introduction To Local Storage

Filled-in Formsremember author information...

try: autoStorage Savify

13年7月19⽇日星期五

Page 51: A Brief Introduction To Local Storage

Filled-in Formsremember author information...

try: autoStorage Savify

13年7月19⽇日星期五

Page 54: A Brief Introduction To Local Storage

Client Cache

• cache API results

• cache AJAX results

Specialized libraries: lscache, Inject, Basket.js

13年7月19⽇日星期五

Page 55: A Brief Introduction To Local Storage

Client Cache

• cache API results

• cache AJAX results

• cache resources

Specialized libraries: lscache, Inject, Basket.js

13年7月19⽇日星期五

Page 57: A Brief Introduction To Local Storage

Abusage• serialize unnecessarily

Read more: localStorage performance

13年7月19⽇日星期五

Page 58: A Brief Introduction To Local Storage

Abusage• serialize unnecessarily

• excessive keys

Read more: localStorage performance

13年7月19⽇日星期五

Page 59: A Brief Introduction To Local Storage

Abusage• serialize unnecessarily

• excessive keys

• excessive gets/sets

Read more: localStorage performance

13年7月19⽇日星期五

Page 60: A Brief Introduction To Local Storage

Abusage• serialize unnecessarily

• excessive keys

• excessive gets/sets

• block the UI

Read more: localStorage performance

13年7月19⽇日星期五

Page 61: A Brief Introduction To Local Storage

Abusage• serialize unnecessarily

• excessive keys

• excessive gets/sets

• block the UI

• assume localStorage always works

Read more: localStorage performance

13年7月19⽇日星期五

Page 62: A Brief Introduction To Local Storage

Abusage• serialize unnecessarily

• excessive keys

• excessive gets/sets

• block the UI

• assume localStorage always works

• use key names that collide

Read more: localStorage performance

13年7月19⽇日星期五

Page 64: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybefore:

function store(key, val) { localStorage.setItem(key, JSON.stringify(val));}store('num', 1);store('on', true);store('name', 'pamela');

try: Primitives vs. Strings JSON.stringify

13年7月19⽇日星期五

Page 65: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybefore:

function store(key, val) { localStorage.setItem(key, JSON.stringify(val));}store('num', 1);store('on', true);store('name', 'pamela');

after:function store(key, val) { localStorage.setItem(key, val);}store('num', '1');store('on', 'true');store('name', 'pamela');

try: Primitives vs. Strings JSON.stringify

13年7月19⽇日星期五

Page 66: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybefore:

function store(key, val) { localStorage.setItem(key, JSON.stringify(val));}store('num', 1);store('on', true);store('name', 'pamela');

after:function store(key, val) { localStorage.setItem(key, val);}store('num', '1');store('on', 'true');store('name', 'pamela');

try: Primitives vs. Strings JSON.stringify

use strings where possible

13年7月19⽇日星期五

Page 67: A Brief Introduction To Local Storage

Don’t Serialize Unnessarily

try: String split/join vs. JSON Stringify

13年7月19⽇日星期五

Page 68: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybefore:

var fruits = ['apple', 'banana', 'orange'];localStorage.setItem('fruits', fruits.join(','));var fruits = localStorage.getItem('fruits').split(',');

try: String split/join vs. JSON Stringify

13年7月19⽇日星期五

Page 69: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybefore:

var fruits = ['apple', 'banana', 'orange'];localStorage.setItem('fruits', fruits.join(','));var fruits = localStorage.getItem('fruits').split(',');

after:

var fruits = ['apple', 'banana', 'orange'];localStorage.setItem('fruits', JSON.stringify(fruits));var fruits = JSON.parse(localStorage.getItem('fruits'));

try: String split/join vs. JSON Stringify

13年7月19⽇日星期五

Page 70: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybefore:

var fruits = ['apple', 'banana', 'orange'];localStorage.setItem('fruits', fruits.join(','));var fruits = localStorage.getItem('fruits').split(',');

after:

var fruits = ['apple', 'banana', 'orange'];localStorage.setItem('fruits', JSON.stringify(fruits));var fruits = JSON.parse(localStorage.getItem('fruits'));

use own serializer if its faster

try: String split/join vs. JSON Stringify

13年7月19⽇日星期五

Page 72: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybefore:

localStorage.setItem('location', JSON.stringify({'state': 'texas', 'city': 'austin'}));

try: Multiple keys vs. JSON2 keys vs. JSON

13年7月19⽇日星期五

Page 73: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybefore:

localStorage.setItem('location', JSON.stringify({'state': 'texas', 'city': 'austin'}));

after:

localStorage.setItem('location-state', 'texas');localStorage.setItem('location-city', 'austin');

try: Multiple keys vs. JSON2 keys vs. JSON

13年7月19⽇日星期五

Page 74: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybefore:

localStorage.setItem('location', JSON.stringify({'state': 'texas', 'city': 'austin'}));

after:

localStorage.setItem('location-state', 'texas');localStorage.setItem('location-city', 'austin');

try: Multiple keys vs. JSON2 keys vs. JSON

store multiple strings instead of 1 object if it is faster

13年7月19⽇日星期五

Page 75: A Brief Introduction To Local Storage

Don’t Serialize Unnessarily

try: 1 long key vs. multiple short keys

13年7月19⽇日星期五

Page 76: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybefore:

localStorage.setItem('first', 'pamela');localStorage.setItem('middle', 'susan');localStorage.setItem('last', 'fox');

try: 1 long key vs. multiple short keys

13年7月19⽇日星期五

Page 77: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybefore:

localStorage.setItem('first', 'pamela');localStorage.setItem('middle', 'susan');localStorage.setItem('last', 'fox');

after:

localStorage.setItem('name', 'pamela susan fox');

try: 1 long key vs. multiple short keys

13年7月19⽇日星期五

Page 78: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybefore:

localStorage.setItem('first', 'pamela');localStorage.setItem('middle', 'susan');localStorage.setItem('last', 'fox');

after:

localStorage.setItem('name', 'pamela susan fox');

try: 1 long key vs. multiple short keys

combine logical keys commonly accessed together

13年7月19⽇日星期五

Page 79: A Brief Introduction To Local Storage

Don’t Serialize Unnessarily

try: Caching in local memoryCaching in the DOM

13年7月19⽇日星期五

Page 80: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybefore:

$('input[type="checkbox"]').click(function() { localStorage.setItem($(this).attr('name'), $(this).is(':checked'));});

try: Caching in local memoryCaching in the DOM

13年7月19⽇日星期五

Page 81: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybefore:

$('input[type="checkbox"]').click(function() { localStorage.setItem($(this).attr('name'), $(this).is(':checked'));});

after:window.onunload = function() { $('input[type="checkbox"]').each(function() { localStorage.setItem($(this).attr('name'), $(this).is(':checked')); });};

try: Caching in local memoryCaching in the DOM

13年7月19⽇日星期五

Page 82: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybefore:

$('input[type="checkbox"]').click(function() { localStorage.setItem($(this).attr('name'), $(this).is(':checked'));});

after:window.onunload = function() { $('input[type="checkbox"]').each(function() { localStorage.setItem($(this).attr('name'), $(this).is(':checked')); });};

try: Caching in local memoryCaching in the DOM

cache data in local memory/DOM,and only get/set on window load/unload

13年7月19⽇日星期五

Page 84: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybefore:

<head><script> $('#name').html(localStorage.getItem('name'));</script></head>

try: Not blocking the UI in tight js loops

13年7月19⽇日星期五

Page 85: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybefore:

<head><script> $('#name').html(localStorage.getItem('name'));</script></head>

after:<body></body><script>window.onload = function() { $('#name').html(localStorage.getItem('name'));};</script>

try: Not blocking the UI in tight js loops

13年7月19⽇日星期五

Page 86: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybefore:

<head><script> $('#name').html(localStorage.getItem('name'));</script></head>

after:<body></body><script>window.onload = function() { $('#name').html(localStorage.getItem('name'));};</script>

try: Not blocking the UI in tight js loops

defer using localStorage until onload

13年7月19⽇日星期五

Page 87: A Brief Introduction To Local Storage

Don’t Serialize Unnessarily

try: Nicholas Zakas: Responsive Interfaces

13年7月19⽇日星期五

Page 88: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybefore:

$('button').click(function() { var name = localStorage.getItem('name'); $('#name').html(name);});

try: Nicholas Zakas: Responsive Interfaces

13年7月19⽇日星期五

Page 89: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybefore:

$('button').click(function() { var name = localStorage.getItem('name'); $('#name').html(name);});

after:$('button').click(function() { window.setTimeout(function() { var name = localStorage.getItem('name'); $('#name').html(name); }, 10);});

try: Nicholas Zakas: Responsive Interfaces

13年7月19⽇日星期五

Page 90: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybefore:

$('button').click(function() { var name = localStorage.getItem('name'); $('#name').html(name);});

after:$('button').click(function() { window.setTimeout(function() { var name = localStorage.getItem('name'); $('#name').html(name); }, 10);});

try: Nicholas Zakas: Responsive Interfaces

use setTimeout to defer localStorage access

13年7月19⽇日星期五

Page 92: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybefore:

$('textarea').keydown(function() { localStorage.setItem('text', $(this).text());});

try: jQuery throttle/debounce plugin2 localStorage tips

13年7月19⽇日星期五

Page 93: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybefore:

$('textarea').keydown(function() { localStorage.setItem('text', $(this).text());});

after:$('textarea').keydown(function() { $.debounce(250, function() { localStorage.setItem('text', $(this).text()); });});

try: jQuery throttle/debounce plugin2 localStorage tips

13年7月19⽇日星期五

Page 94: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybefore:

$('textarea').keydown(function() { localStorage.setItem('text', $(this).text());});

after:$('textarea').keydown(function() { $.debounce(250, function() { localStorage.setItem('text', $(this).text()); });});

try: jQuery throttle/debounce plugin2 localStorage tips

throttle or debounce to avoid repetitive gets/sets

13年7月19⽇日星期五

Page 95: A Brief Introduction To Local Storage

Don’t Serialize Unnessarily

13年7月19⽇日星期五

Page 96: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybad:

localStorage.setItem('bla', 'bla');

13年7月19⽇日星期五

Page 97: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybad:

localStorage.setItem('bla', 'bla');

better:if (window.localStorage) { localStorage.setItem('bla', 'bla');}

13年7月19⽇日星期五

Page 98: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybad:

localStorage.setItem('bla', 'bla');

better:if (window.localStorage) { localStorage.setItem('bla', 'bla');}

best:if (window.localStorage) { try { localStorage.setItem('bla', 'bla'); } catch(e) { if (e.name === 'QUOTA_EXCEEDED_ERR' || e.name === 'NS_ERROR_DOM_QUOTA_REACHED') { } else { } }}

13年7月19⽇日星期五

Page 99: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybad:

localStorage.setItem('bla', 'bla');

better:if (window.localStorage) { localStorage.setItem('bla', 'bla');}

best:if (window.localStorage) { try { localStorage.setItem('bla', 'bla'); } catch(e) { if (e.name === 'QUOTA_EXCEEDED_ERR' || e.name === 'NS_ERROR_DOM_QUOTA_REACHED') { } else { } }}

check for feature support

13年7月19⽇日星期五

Page 100: A Brief Introduction To Local Storage

Don’t Serialize Unnessarily

try: long vs. short names

13年7月19⽇日星期五

Page 101: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybad:

localStorage.setItem('name', 'pamela');

try: long vs. short names

13年7月19⽇日星期五

Page 102: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybad:

localStorage.setItem('name', 'pamela');

better:localStorage.setItem('conference-speaker-first-name', 'pamela');

try: long vs. short names

13年7月19⽇日星期五

Page 103: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybad:

localStorage.setItem('name', 'pamela');

better:localStorage.setItem('conference-speaker-first-name', 'pamela');

best:lscache.setBucket('conference-speaker');lscache.set('first-name', 'pamela');

try: long vs. short names

13年7月19⽇日星期五

Page 104: A Brief Introduction To Local Storage

Don’t Serialize Unnessarilybad:

localStorage.setItem('name', 'pamela');

better:localStorage.setItem('conference-speaker-first-name', 'pamela');

best:lscache.setBucket('conference-speaker');lscache.set('first-name', 'pamela');

try: long vs. short names

use highliy descriptive keys with pseudo namespaces

13年7月19⽇日星期五

Page 105: A Brief Introduction To Local Storage

QA

13年7月19⽇日星期五