SlideShare une entreprise Scribd logo
1  sur  37
Prepare yourselves




The Mobile has arrived
Go where no website has gone before
Scotty, beam me up




I have good internet bandwidth
Darth Vader Says
Work when you are disconnected too
What if I told you




that adding Offline support is easy ?
A simple website
                   DATA in
                   SERVER




                    DATA in
                   BROWSER
SHOW ME
http://nparashuram.com/conference
Simple Read Operation
AjaxHandler = {
    getSession: function (id) {
        // code goes here
    },
    getAllSessions: function () {
        // code goes here
    }
}


  var Session = Backbone.Model.extend({});
  var SessionList = Backbone.Collection.extend({});

  var singleSession = new Session();
  singleSession.get(101);
  view.update(singleSession);

  var allSessions = new SessionList();
  allSessions.fetch();
  view.render(allSessions);
Backbone.sync = function (method, model, options, error)
{
    switch (method) {
        case "read":
            // Methods for reading from database
            break;
        case "create":
           // Create a record here
            break;
        case "update":
           // Update Records
            break;
        case "delete":
          // Delete record
    }
RemoteDatabase.replicate();
});
case "read":// Pseudo Code
if (model.id) {// for a single Model
    db.get(model.id,
        function (err, res) {
         if (err) {
             error(err);
             return;
         }
         options.success(res);
        }
    );
} else {    // For all rows in the DB
    db.getAll (
       function (err, resp) {
         if (err) {
             error(err);
             return;
         }
         options.success(resp);
       }
    );
}
With all that pseudo code




 I totally understand you
case "read": // Real IndexedDB Code
if (model.id) {// for a single Model
    var openReq = window.indexedDB.open("DatabaseName");
    openReq.onsuccess = function () {
        var db = openReq.result;
        var transaction = db.transaction(["Model1”], "readonly");
        var objectStore = transaction.objectStore("Model1");
        var readReq = objectStore.get(model.id);
        readReq.onsuccess = function () {
            option.success(readReq.result);
        };
    };
    openReq.onerror = function (e) {
        options.error(e);
    };
}
What did you just do ?
IndexedDB Refresher
     http://yourwebpage.com                                               search



               Database                               Database
  Transaction
  Object Store                             Object Store

   key : value                 Index        key : value           Index
   key : value                              key : value
                              Cursor on                          Cursor on
   key : value                  Index       key : value            Index

 Cursor on Object                         Cursor on Object
      Store                                    Store
case "read":

if (model.id) {
    var openReq = window.indexedDB.open("DatabaseName");
    openReq.onsuccess = function () {
        var db = openReq.result;
        var transaction = db.transaction(["Model1”], "readonly");
        var objectStore = transaction.objectStore("Model1");
        var readReq = objectStore.get(model.id);
        readReq.onsuccess = function () {
            option.success(readReq.result);
        };
    };
    openReq.onerror = function (e) {
        options.error(e);
    };
}
You wrote so much code




Just to read a single record ?
case "read": // Jquery-IndexedDB Plugin

if (model.id) {
    var openReq = window.indexedDB.open("DatabaseName");
    $.indexedDB("DatabaseName")
    openReq.onsuccess = function () {
        var db = openReq.result;
        var transaction = db.transaction(["Model1”], "readonly");
        var objectStore = transaction.objectStore("Model1");
       .objectStore("Model1")
       .get(model.id) objectStore.get(model.id);
        var readReq =
        readReq.onsuccess = function () {
       .done(function(data) {
            option.success(readReq.result);
        };
       })
        readReq.onerror = function (e) {
       .fail(function (error) {
             options.error(e);
        };
       });
    };
    openReq.onerror = function (e) {
        options.error(e);
    };
}
case "read": // Jquery-IndexedDB Plugin
$.indexedDB("DatabaseName")
    .objectStore("Model1")
    .get(model.id)                     •   Less Verbose
    .done(function (data) {
        option.success(data);          •   Chainable API
    }).fail(function (error) {
        option.error(error);
                                       •   Use Promises
    });                                •   Smart Defaults
                                       •   Almost transparent
$.indexedDB("DatabaseName")
    .objectStore("Model1")
    .each(function (record) {
        display(record);
    }).done(function () {
        // Finished with all records
                                       Project :
    }).fail(function () {              gitbhub
        // Error
    });                                /axemclion
                                       /jquery-indexeddb
case "read":// Pouch DB Code
 if (model.id) {// for a single Model
     db.get(model.id, {},
         function (err, res) {
          if (err) {
              error(err);
              return;
          }
          options.success(res);
         }
     );
 } else {    // For all rows in the DB
     db.allDocs ({},
        function (err, resp) {
          if (err) {
              error(err);
              return;
          }
          options.success(resp);
        }
     );
Pouch.replicate("idb://data","http://me.couchdb.com/data");
 }
Sure, but can you query data ?
Querying IndexedDB

        $.indexedDB("DatabaseName")
            .objectStore("Model1")
            .where('price < 100')
            .sortBy('price')
            .desc()




         objectStore.index('price').openCursor(
             IDBKeyRange.lowerBound(100, false),
             "prev"
         );
Querying IndexedDB

        $.indexedDB("DatabaseName")
            .objectStore("Model1")
            .where('price < 100 and >= 200')
            .sortBy('price')
            .desc()


        objectStore.index('price').openCursor(
            IDBKeyRange.bound(100, 200, false, true),
            "prev"
        );
Querying IndexedDB
var results = $.indexedDB("DatabaseName")
                  .objectStore("Model1")
                  .where('price < 100 and >= 200')
                  .sortBy('name')
                  .desc()
                  .done(function(results){
                      // do something with results
                      // results.length == ???
                  })
         objectStore.index('price').openCursor(
             IDBKeyRange.bound(100, 200, false, true),
             "prev"
         );
Querying IndexedDB
                  $.indexedDB("DatabaseName")
                        .objectStore("Model1")
                        .where('price < 100 and >= 200')
                        .where('ratings > 4')
                        .sortBy('name')
                        .desc()
                        .each(function(result){
                       .done(function(results){
                                 process each result
                             // do something with results
                        }) // results.length == ???
cursorReq =              })
            objectStore.index('name').openCursor();
cursorReq.onsuccess = function () {
    if (cursorReq.result) {
        val = cursorReq.result.value;
        if (val.price < 100 && val.price > 200 && val.ratings > 4)
                                               )
           callback(val);
        cursorReq.result.continue();
    }
}
Querying IndexedDB
          $.indexedDB("DatabaseName")
              .objectStore("Model1")
              .where('price < 100 and >= 200')
              .where(‘Model2.empId < 5')
              .sortBy('name')
              .sortBy('price')
              .each(function(result){
                  // process each result
              });
Querying IndexedDB
                       $.indexedDB("DatabaseName")
                           .objectStore("Model1")
                           .where('price < 100 and >= 200')
                           .where(‘Model2.empId < 5 ')
                           .sortBy('name')
                           .sortBy('price')
                           .each(function(result){
                               // process each result
                           });
 Fall back on Web Workers to do the job

 • Sorting on multiple columns
 • Where clauses across object stores
 • Sorting and clauses across object stores



        Project : //linq2indexeddb.codeplex.com/
ONE DOES NOT SIMPLY START USING INDEXEDDB




    WITHOUT LOOKING AT PERFORMANCE
Comparing IndexedDB and WebSQL ?
Performance Analysis
• Common cases when writing an application
   • Are string keys slower than integer keys
   • Should I combine read and write transactions ?
• Started with JSPerf, Problems with Async Setup
   • Using Benchmarkjs




  Thanks to @slace for helping with code reviews
http://nparashuram.com/IndexedDB/perf
May the force be with you
     http://nparashuram.com
    @nparashuram

           Resources
   @OpenAtMicrosoft
      (@obloch)
http://nparashuram.com/IndexedDB
    http://www.html5labs.com/

Contenu connexe

Tendances

AngularJS Architecture
AngularJS ArchitectureAngularJS Architecture
AngularJS ArchitectureEyal Vardi
 
AngularJS for designers and developers
AngularJS for designers and developersAngularJS for designers and developers
AngularJS for designers and developersKai Koenig
 
[FEConf Korea 2017]Angular 컴포넌트 대화법
[FEConf Korea 2017]Angular 컴포넌트 대화법[FEConf Korea 2017]Angular 컴포넌트 대화법
[FEConf Korea 2017]Angular 컴포넌트 대화법Jeado Ko
 
Beyond DOMReady: Ultra High-Performance Javascript
Beyond DOMReady: Ultra High-Performance JavascriptBeyond DOMReady: Ultra High-Performance Javascript
Beyond DOMReady: Ultra High-Performance Javascriptaglemann
 
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasmineSingle Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasminePaulo Ragonha
 
AngularJs $provide API internals & circular dependency problem.
AngularJs $provide API internals & circular dependency problem.AngularJs $provide API internals & circular dependency problem.
AngularJs $provide API internals & circular dependency problem.Yan Yankowski
 
Angular js routing options
Angular js routing optionsAngular js routing options
Angular js routing optionsNir Kaufman
 
Intro to Ember.js
Intro to Ember.jsIntro to Ember.js
Intro to Ember.jsJay Phelps
 
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)Igor Bronovskyy
 
Building scalable applications with angular js
Building scalable applications with angular jsBuilding scalable applications with angular js
Building scalable applications with angular jsAndrew Alpert
 
Modules and injector
Modules and injectorModules and injector
Modules and injectorEyal Vardi
 
Viking academy backbone.js
Viking academy  backbone.jsViking academy  backbone.js
Viking academy backbone.jsBert Wijnants
 
Workshop 26: React Native - The Native Side
Workshop 26: React Native - The Native SideWorkshop 26: React Native - The Native Side
Workshop 26: React Native - The Native SideVisual Engineering
 
Javascript Module Patterns
Javascript Module PatternsJavascript Module Patterns
Javascript Module PatternsNicholas Jansma
 
Angular JS blog tutorial
Angular JS blog tutorialAngular JS blog tutorial
Angular JS blog tutorialClaude Tech
 

Tendances (20)

AngularJS Architecture
AngularJS ArchitectureAngularJS Architecture
AngularJS Architecture
 
AngularJS for designers and developers
AngularJS for designers and developersAngularJS for designers and developers
AngularJS for designers and developers
 
[FEConf Korea 2017]Angular 컴포넌트 대화법
[FEConf Korea 2017]Angular 컴포넌트 대화법[FEConf Korea 2017]Angular 컴포넌트 대화법
[FEConf Korea 2017]Angular 컴포넌트 대화법
 
AngularJs
AngularJsAngularJs
AngularJs
 
Beyond DOMReady: Ultra High-Performance Javascript
Beyond DOMReady: Ultra High-Performance JavascriptBeyond DOMReady: Ultra High-Performance Javascript
Beyond DOMReady: Ultra High-Performance Javascript
 
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasmineSingle Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
 
AngularJs $provide API internals & circular dependency problem.
AngularJs $provide API internals & circular dependency problem.AngularJs $provide API internals & circular dependency problem.
AngularJs $provide API internals & circular dependency problem.
 
Angular js routing options
Angular js routing optionsAngular js routing options
Angular js routing options
 
Intro to Ember.js
Intro to Ember.jsIntro to Ember.js
Intro to Ember.js
 
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
 
Building scalable applications with angular js
Building scalable applications with angular jsBuilding scalable applications with angular js
Building scalable applications with angular js
 
Workshop 17: EmberJS parte II
Workshop 17: EmberJS parte IIWorkshop 17: EmberJS parte II
Workshop 17: EmberJS parte II
 
Modules and injector
Modules and injectorModules and injector
Modules and injector
 
Viking academy backbone.js
Viking academy  backbone.jsViking academy  backbone.js
Viking academy backbone.js
 
Workshop 26: React Native - The Native Side
Workshop 26: React Native - The Native SideWorkshop 26: React Native - The Native Side
Workshop 26: React Native - The Native Side
 
Intro to Ember.JS 2016
Intro to Ember.JS 2016Intro to Ember.JS 2016
Intro to Ember.JS 2016
 
Javascript Module Patterns
Javascript Module PatternsJavascript Module Patterns
Javascript Module Patterns
 
AngularJS Basics with Example
AngularJS Basics with ExampleAngularJS Basics with Example
AngularJS Basics with Example
 
Angular JS blog tutorial
Angular JS blog tutorialAngular JS blog tutorial
Angular JS blog tutorial
 
Angular js
Angular jsAngular js
Angular js
 

Similaire à IndexedDB - Querying and Performance

Persistent Memoization with HTML5 indexedDB and jQuery Promises
Persistent Memoization with HTML5 indexedDB and jQuery PromisesPersistent Memoization with HTML5 indexedDB and jQuery Promises
Persistent Memoization with HTML5 indexedDB and jQuery PromisesRay Bellis
 
Contagion的Ruby/Rails投影片
Contagion的Ruby/Rails投影片Contagion的Ruby/Rails投影片
Contagion的Ruby/Rails投影片cfc
 
Mongoskin - Guilin
Mongoskin - GuilinMongoskin - Guilin
Mongoskin - GuilinJackson Tian
 
Taming that client side mess with Backbone.js
Taming that client side mess with Backbone.jsTaming that client side mess with Backbone.js
Taming that client side mess with Backbone.jsJarod Ferguson
 
Scaling php applications with redis
Scaling php applications with redisScaling php applications with redis
Scaling php applications with redisjimbojsb
 
Intro to IndexedDB (Beta)
Intro to IndexedDB (Beta)Intro to IndexedDB (Beta)
Intro to IndexedDB (Beta)Mike West
 
Javascript first-class citizenery
Javascript first-class citizeneryJavascript first-class citizenery
Javascript first-class citizenerytoddbr
 
What do you mean, Backwards Compatibility?
What do you mean, Backwards Compatibility?What do you mean, Backwards Compatibility?
What do you mean, Backwards Compatibility?Trisha Gee
 
Dependency Management with RequireJS
Dependency Management with RequireJSDependency Management with RequireJS
Dependency Management with RequireJSAaronius
 
NoSQL and JavaScript: a Love Story
NoSQL and JavaScript: a Love StoryNoSQL and JavaScript: a Love Story
NoSQL and JavaScript: a Love StoryAlexandre Morgaut
 
Drupal II: The SQL
Drupal II: The SQLDrupal II: The SQL
Drupal II: The SQLddiers
 
jQuery and Rails: Best Friends Forever
jQuery and Rails: Best Friends ForeverjQuery and Rails: Best Friends Forever
jQuery and Rails: Best Friends Foreverstephskardal
 
JavaScript - i och utanför webbläsaren (2010-03-03)
JavaScript - i och utanför webbläsaren (2010-03-03)JavaScript - i och utanför webbläsaren (2010-03-03)
JavaScript - i och utanför webbläsaren (2010-03-03)Anders Jönsson
 
Future of Web Apps: Google Gears
Future of Web Apps: Google GearsFuture of Web Apps: Google Gears
Future of Web Apps: Google Gearsdion
 
Understanding backbonejs
Understanding backbonejsUnderstanding backbonejs
Understanding backbonejsNick Lee
 
Node js mongodriver
Node js mongodriverNode js mongodriver
Node js mongodriverchristkv
 
Building an api using golang and postgre sql v1.0
Building an api using golang and postgre sql v1.0Building an api using golang and postgre sql v1.0
Building an api using golang and postgre sql v1.0Frost
 

Similaire à IndexedDB - Querying and Performance (20)

Persistent Memoization with HTML5 indexedDB and jQuery Promises
Persistent Memoization with HTML5 indexedDB and jQuery PromisesPersistent Memoization with HTML5 indexedDB and jQuery Promises
Persistent Memoization with HTML5 indexedDB and jQuery Promises
 
Indexed db
Indexed dbIndexed db
Indexed db
 
Contagion的Ruby/Rails投影片
Contagion的Ruby/Rails投影片Contagion的Ruby/Rails投影片
Contagion的Ruby/Rails投影片
 
Mongoskin - Guilin
Mongoskin - GuilinMongoskin - Guilin
Mongoskin - Guilin
 
Taming that client side mess with Backbone.js
Taming that client side mess with Backbone.jsTaming that client side mess with Backbone.js
Taming that client side mess with Backbone.js
 
Scaling php applications with redis
Scaling php applications with redisScaling php applications with redis
Scaling php applications with redis
 
Intro to IndexedDB (Beta)
Intro to IndexedDB (Beta)Intro to IndexedDB (Beta)
Intro to IndexedDB (Beta)
 
DataMapper
DataMapperDataMapper
DataMapper
 
Javascript first-class citizenery
Javascript first-class citizeneryJavascript first-class citizenery
Javascript first-class citizenery
 
JavaScript JQUERY AJAX
JavaScript JQUERY AJAXJavaScript JQUERY AJAX
JavaScript JQUERY AJAX
 
What do you mean, Backwards Compatibility?
What do you mean, Backwards Compatibility?What do you mean, Backwards Compatibility?
What do you mean, Backwards Compatibility?
 
Dependency Management with RequireJS
Dependency Management with RequireJSDependency Management with RequireJS
Dependency Management with RequireJS
 
NoSQL and JavaScript: a Love Story
NoSQL and JavaScript: a Love StoryNoSQL and JavaScript: a Love Story
NoSQL and JavaScript: a Love Story
 
Drupal II: The SQL
Drupal II: The SQLDrupal II: The SQL
Drupal II: The SQL
 
jQuery and Rails: Best Friends Forever
jQuery and Rails: Best Friends ForeverjQuery and Rails: Best Friends Forever
jQuery and Rails: Best Friends Forever
 
JavaScript - i och utanför webbläsaren (2010-03-03)
JavaScript - i och utanför webbläsaren (2010-03-03)JavaScript - i och utanför webbläsaren (2010-03-03)
JavaScript - i och utanför webbläsaren (2010-03-03)
 
Future of Web Apps: Google Gears
Future of Web Apps: Google GearsFuture of Web Apps: Google Gears
Future of Web Apps: Google Gears
 
Understanding backbonejs
Understanding backbonejsUnderstanding backbonejs
Understanding backbonejs
 
Node js mongodriver
Node js mongodriverNode js mongodriver
Node js mongodriver
 
Building an api using golang and postgre sql v1.0
Building an api using golang and postgre sql v1.0Building an api using golang and postgre sql v1.0
Building an api using golang and postgre sql v1.0
 

Plus de Parashuram N

React Native - Fabric review-2018-07-25
React Native - Fabric review-2018-07-25React Native - Fabric review-2018-07-25
React Native - Fabric review-2018-07-25Parashuram N
 
Chain React 2018 - The state of React Native
Chain React 2018 - The state of React NativeChain React 2018 - The state of React Native
Chain React 2018 - The state of React NativeParashuram N
 
Understanding Javascript Engines
Understanding Javascript Engines Understanding Javascript Engines
Understanding Javascript Engines Parashuram N
 
Indexed db - the store in the browser
Indexed db - the store in the browserIndexed db - the store in the browser
Indexed db - the store in the browserParashuram N
 
DoctypeHTML5 (Hyderabad) Presentation on Multimedia
DoctypeHTML5 (Hyderabad) Presentation on MultimediaDoctypeHTML5 (Hyderabad) Presentation on Multimedia
DoctypeHTML5 (Hyderabad) Presentation on MultimediaParashuram N
 

Plus de Parashuram N (8)

React Native - Fabric review-2018-07-25
React Native - Fabric review-2018-07-25React Native - Fabric review-2018-07-25
React Native - Fabric review-2018-07-25
 
Chain React 2018 - The state of React Native
Chain React 2018 - The state of React NativeChain React 2018 - The state of React Native
Chain React 2018 - The state of React Native
 
Client storage
Client storageClient storage
Client storage
 
Understanding Javascript Engines
Understanding Javascript Engines Understanding Javascript Engines
Understanding Javascript Engines
 
Indexed DB
Indexed DBIndexed DB
Indexed DB
 
Indexed db - the store in the browser
Indexed db - the store in the browserIndexed db - the store in the browser
Indexed db - the store in the browser
 
DoctypeHTML5 (Hyderabad) Presentation on Multimedia
DoctypeHTML5 (Hyderabad) Presentation on MultimediaDoctypeHTML5 (Hyderabad) Presentation on Multimedia
DoctypeHTML5 (Hyderabad) Presentation on Multimedia
 
Snapp
SnappSnapp
Snapp
 

IndexedDB - Querying and Performance

  • 1.
  • 2.
  • 4. Go where no website has gone before
  • 5.
  • 6. Scotty, beam me up I have good internet bandwidth
  • 7. Darth Vader Says Work when you are disconnected too
  • 8. What if I told you that adding Offline support is easy ?
  • 9. A simple website DATA in SERVER DATA in BROWSER
  • 12. Simple Read Operation AjaxHandler = { getSession: function (id) { // code goes here }, getAllSessions: function () { // code goes here } } var Session = Backbone.Model.extend({}); var SessionList = Backbone.Collection.extend({}); var singleSession = new Session(); singleSession.get(101); view.update(singleSession); var allSessions = new SessionList(); allSessions.fetch(); view.render(allSessions);
  • 13. Backbone.sync = function (method, model, options, error) { switch (method) { case "read": // Methods for reading from database break; case "create": // Create a record here break; case "update": // Update Records break; case "delete": // Delete record } RemoteDatabase.replicate(); });
  • 14. case "read":// Pseudo Code if (model.id) {// for a single Model db.get(model.id, function (err, res) { if (err) { error(err); return; } options.success(res); } ); } else { // For all rows in the DB db.getAll ( function (err, resp) { if (err) { error(err); return; } options.success(resp); } ); }
  • 15. With all that pseudo code I totally understand you
  • 16. case "read": // Real IndexedDB Code if (model.id) {// for a single Model var openReq = window.indexedDB.open("DatabaseName"); openReq.onsuccess = function () { var db = openReq.result; var transaction = db.transaction(["Model1”], "readonly"); var objectStore = transaction.objectStore("Model1"); var readReq = objectStore.get(model.id); readReq.onsuccess = function () { option.success(readReq.result); }; }; openReq.onerror = function (e) { options.error(e); }; }
  • 17. What did you just do ?
  • 18. IndexedDB Refresher http://yourwebpage.com search Database Database Transaction Object Store Object Store key : value Index key : value Index key : value key : value Cursor on Cursor on key : value Index key : value Index Cursor on Object Cursor on Object Store Store
  • 19. case "read": if (model.id) { var openReq = window.indexedDB.open("DatabaseName"); openReq.onsuccess = function () { var db = openReq.result; var transaction = db.transaction(["Model1”], "readonly"); var objectStore = transaction.objectStore("Model1"); var readReq = objectStore.get(model.id); readReq.onsuccess = function () { option.success(readReq.result); }; }; openReq.onerror = function (e) { options.error(e); }; }
  • 20. You wrote so much code Just to read a single record ?
  • 21. case "read": // Jquery-IndexedDB Plugin if (model.id) { var openReq = window.indexedDB.open("DatabaseName"); $.indexedDB("DatabaseName") openReq.onsuccess = function () { var db = openReq.result; var transaction = db.transaction(["Model1”], "readonly"); var objectStore = transaction.objectStore("Model1"); .objectStore("Model1") .get(model.id) objectStore.get(model.id); var readReq = readReq.onsuccess = function () { .done(function(data) { option.success(readReq.result); }; }) readReq.onerror = function (e) { .fail(function (error) { options.error(e); }; }); }; openReq.onerror = function (e) { options.error(e); }; }
  • 22. case "read": // Jquery-IndexedDB Plugin $.indexedDB("DatabaseName") .objectStore("Model1") .get(model.id) • Less Verbose .done(function (data) { option.success(data); • Chainable API }).fail(function (error) { option.error(error); • Use Promises }); • Smart Defaults • Almost transparent $.indexedDB("DatabaseName") .objectStore("Model1") .each(function (record) { display(record); }).done(function () { // Finished with all records Project : }).fail(function () { gitbhub // Error }); /axemclion /jquery-indexeddb
  • 23. case "read":// Pouch DB Code if (model.id) {// for a single Model db.get(model.id, {}, function (err, res) { if (err) { error(err); return; } options.success(res); } ); } else { // For all rows in the DB db.allDocs ({}, function (err, resp) { if (err) { error(err); return; } options.success(resp); } ); Pouch.replicate("idb://data","http://me.couchdb.com/data"); }
  • 24. Sure, but can you query data ?
  • 25. Querying IndexedDB $.indexedDB("DatabaseName") .objectStore("Model1") .where('price < 100') .sortBy('price') .desc() objectStore.index('price').openCursor( IDBKeyRange.lowerBound(100, false), "prev" );
  • 26. Querying IndexedDB $.indexedDB("DatabaseName") .objectStore("Model1") .where('price < 100 and >= 200') .sortBy('price') .desc() objectStore.index('price').openCursor( IDBKeyRange.bound(100, 200, false, true), "prev" );
  • 27. Querying IndexedDB var results = $.indexedDB("DatabaseName") .objectStore("Model1") .where('price < 100 and >= 200') .sortBy('name') .desc() .done(function(results){ // do something with results // results.length == ??? }) objectStore.index('price').openCursor( IDBKeyRange.bound(100, 200, false, true), "prev" );
  • 28. Querying IndexedDB $.indexedDB("DatabaseName") .objectStore("Model1") .where('price < 100 and >= 200') .where('ratings > 4') .sortBy('name') .desc() .each(function(result){ .done(function(results){ process each result // do something with results }) // results.length == ??? cursorReq = }) objectStore.index('name').openCursor(); cursorReq.onsuccess = function () { if (cursorReq.result) { val = cursorReq.result.value; if (val.price < 100 && val.price > 200 && val.ratings > 4) ) callback(val); cursorReq.result.continue(); } }
  • 29. Querying IndexedDB $.indexedDB("DatabaseName") .objectStore("Model1") .where('price < 100 and >= 200') .where(‘Model2.empId < 5') .sortBy('name') .sortBy('price') .each(function(result){ // process each result });
  • 30.
  • 31. Querying IndexedDB $.indexedDB("DatabaseName") .objectStore("Model1") .where('price < 100 and >= 200') .where(‘Model2.empId < 5 ') .sortBy('name') .sortBy('price') .each(function(result){ // process each result }); Fall back on Web Workers to do the job • Sorting on multiple columns • Where clauses across object stores • Sorting and clauses across object stores Project : //linq2indexeddb.codeplex.com/
  • 32. ONE DOES NOT SIMPLY START USING INDEXEDDB WITHOUT LOOKING AT PERFORMANCE
  • 34.
  • 35. Performance Analysis • Common cases when writing an application • Are string keys slower than integer keys • Should I combine read and write transactions ? • Started with JSPerf, Problems with Async Setup • Using Benchmarkjs Thanks to @slace for helping with code reviews
  • 37. May the force be with you http://nparashuram.com @nparashuram Resources @OpenAtMicrosoft (@obloch) http://nparashuram.com/IndexedDB http://www.html5labs.com/