Skip to content

Instantly share code, notes, and snippets.

@hnq90
Created June 9, 2017 07:39
Show Gist options
  • Save hnq90/972f6597a0927f45d9075b8627892783 to your computer and use it in GitHub Desktop.
Save hnq90/972f6597a0927f45d9075b8627892783 to your computer and use it in GitHub Desktop.
PouchDB benchmark
import dream from 'dreamjs'
import PouchDB from 'pouchdb-react-native'
import SQLite from 'react-native-sqlite-2'
import SQLiteAdapterFactory from 'pouchdb-adapter-react-native-sqlite'
const SQLiteAdapter = SQLiteAdapterFactory(SQLite)
PouchDB
.plugin(SQLiteAdapter)
.plugin(require('pouchdb-adapter-asyncstorage').default)
.plugin(require('pouchdb-find'))
function toTime (totalMilliSecond) {
let ms = totalMilliSecond % 1000
totalMilliSecond = (totalMilliSecond - ms) / 1000
let secs = totalMilliSecond % 60
totalMilliSecond = (totalMilliSecond - secs) / 60
let mins = totalMilliSecond % 60
let hrs = (totalMilliSecond - mins) / 60
hrs = (hrs < 10) ? '0' + hrs : hrs
mins = (mins < 10) ? '0' + mins : mins
secs = (secs < 10) ? '0' + secs : secs
return hrs + ':' + mins + ':' + secs + '.' + ms
}
const NUMBER_RECORD = 5000
const sqliteDB = new PouchDB('sqlite_db', { adapter: 'react-native-sqlite' })
const asyncDB = new PouchDB('async_db', { adapter: 'asyncstorage' })
// Populates data
dream
.schema({
name: 'name',
age: 'age',
address: 'address',
contact: {
phone: 'phone',
servicePhone: /^(800[1-9]{6})$/
},
gender: 'gender',
email: 'email'
})
.generateRnd(NUMBER_RECORD)
.output((_, result) => {
console.log('Generate fake data complete')
let start1 = (new Date()).getTime()
sqliteDB.bulkDocs(result)
.then(response => {
let end = (new Date()).getTime()
console.log('BENCHMARK: SQLITE - BULKDOCS - ', toTime(end - start1))
// Retrieve DB information
sqliteDB.info().then(result => {
console.log(result)
}).catch(err => {
console.log(err)
})
}).catch(err => {
console.log(err)
})
let start2 = (new Date()).getTime()
asyncDB.bulkDocs(result)
.then(response => {
let end = (new Date()).getTime()
console.log('BENCHMARK: ASYNCSTORAGE - BULKDOCS - ', toTime(end - start2))
// Retrieve DB information
asyncDB.info().then(result => {
console.log(result)
}).catch(err => {
console.log(err)
})
}).catch(err => {
console.log(err)
})
})
let start3 = (new Date()).getTime()
sqliteDB.allDocs({
include_docs: true,
attachments: true
}).then(result => {
let end = (new Date()).getTime()
console.log('BENCHMARK: SQLITE - ALLDOCS - ', toTime(end - start3))
}).catch(err => {
console.log(err)
})
let start4 = (new Date()).getTime()
asyncDB.allDocs({
include_docs: true,
attachments: true
}).then(result => {
let end = (new Date()).getTime()
console.log('BENCHMARK: ASYNCSTORAGE - ALLDOCS - ', toTime(end - start4))
}).catch(err => {
console.log(err)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment