Web Storage
What is it to you?
FileSystem
Local storage
Session storage
Cookies
WebSQL
Cache
IndexedDB

Pros & Cons
Pros
- Supported since IE8
- Easy to use
Cons
- Poor performance
- No indexing
- Only strings
Local/Session Storage
Note : Local and session storage work the same way but session storage is erased when window is closed
Pros
- Supported on major browsers
- Asynchronous / Good performance
- Data can be indexed / Fast search
- Robust (transactional DB model)
Cons
- Not supported by IE nor Firefox
- Needs SQL knowledge
- Rigid / Needs the structure to be defined upfront
Web SQL
Pros
- Asynchronous
- Data indexing / Fast search
- Robust (transactional DB model)
- Soft data model
Cons
- Complex APIĀ
IndexedDB
Pros
- Can store large binary files
- Asynchronous / Good performance
Cons
- Only supported by Chrome and Opera for now
- No transaction
- No indexing
(FileSystem)
Comparison
NgForage
One tool for all storage options
Really easy
import { NgForageConfig } from 'ngforage';
...
@NgModule({
...
})
export class AppModule {
public constructor(
ngfConfig: NgForageConfig
) {
ngfConfig.configure({
name: 'myProject',
driver: [ // optional - defaults to indexedDB -> webSQL -> localStorage -> sessionStorage
NgForageConfig.DRIVER_INDEXEDDB,
NgForageConfig.DRIVER_LOCALSTORAGE
]
});
}
}app.module.ts
import { Injectable, OnInit } from '@angular/core';
import { CachedItem, NgForage, NgForageCache, NgForageConfig } from 'ngforage';
import { Observable, from } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class StorageService implements OnInit {
constructor (
private readonly ngf: NgForage,
private readonly cache: NgForageCache
) { }
public getItem<T> (key: string): Observable<T> {
return from(this.ngf.getItem<T>(key));
}
public setItem<T> (key: string, data: T): Observable<T> {
return from(this.ngf.setItem<T>(key, data));
}
public removeItem (key: string): Observable<void> {
return from(this.ngf.removeItem(key));
}
public removeAll (): Observable<void> {
return from(this.ngf.clear());
}
public getCachedItem<T> (key: string): Observable<T | null> {
return from(this.cache.getCached<T>(key)
.then((r: CachedItem<T>) => {
if (!r.hasData || r.expired) {
return null;
}
return r.data;
}));
}
public setCachedItem<T> (key: string, data: T): Observable<T> {
return from(this.cache.setCached<T>(key, data));
}
public ngOnInit (): void {
this.ngf.name = 'myApp';
this.cache.driver = NgForageConfig.DRIVER_SESSIONSTORAGE;
}
}
Storage.service.ts
Webstorage
By Simon Roussel
Webstorage
- 297