Created
August 13, 2019 07:07
-
-
Save watanabeyu/71262782ba0829dfeb851272eb65adfc to your computer and use it in GitHub Desktop.
performance check test react-native-store / expo-sqlite / expo-sqlite-orm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useEffect } from 'react'; | |
import { StyleSheet, Text, View, TouchableOpacity } from 'react-native'; | |
import { SQLite } from 'expo-sqlite' | |
import { BaseModel, types } from 'expo-sqlite-orm'; | |
import DatabaseLayer from 'expo-sqlite-orm/src/DatabaseLayer'; | |
import Store from 'react-native-store'; | |
const DB = { | |
'foo': Store.model('foo'), | |
'bar': Store.model('bar') | |
} | |
let database = SQLite.openDatabase('db.db'); | |
database.transaction(tx => { | |
tx.executeSql('create table if not exists players (id integer primary key autoincrement, foo text, bar text, created integer);'); | |
}) | |
class Animal extends BaseModel { | |
constructor(obj) { | |
super(obj) | |
} | |
static get database() { | |
return async () => SQLite.openDatabase('database.db') | |
} | |
static get tableName() { | |
return 'animals' | |
} | |
static get columnMapping() { | |
return { | |
id: { type: types.INTEGER, primary_key: true, autoincrement: true, }, | |
foo: { type: types.TEXT }, | |
bar: { type: types.TEXT }, | |
created: { type: types.INTEGER, default: () => Date.now() } | |
} | |
} | |
} | |
Animal.createTable(); | |
export default function App() { | |
const onInsert1Press = async () => { | |
const now = new Date().getMilliseconds(); | |
let data = [] | |
for (let i = 0; i < 10000; i++) { | |
data.push({ | |
id: i, | |
foo: Math.random().toString(32).substring(2), | |
bar: Math.random().toString(32).substring(2), | |
created: new Date().getTime() | |
}) | |
} | |
const res = await DB.foo.multiAdd(data); | |
console.log(`insert : ${new Date().getMilliseconds() - now}ms`); | |
} | |
const onAll1Press = async () => { | |
const now = new Date().getMilliseconds(); | |
await DB.foo.find().then(resp => { | |
// console.log(resp); | |
console.log(`all : ${new Date().getMilliseconds() - now}ms`); | |
}); | |
} | |
const onFind1Press = () => { | |
const now = new Date().getMilliseconds(); | |
DB.foo.find({ | |
where: { | |
and: [{ id: { lte: 30 } }, { bar: { eq: "skqnmk29ps8" } }] | |
}, | |
order: { | |
age: 'ASC', | |
} | |
}).then(resp => { | |
console.log(`find : ${new Date().getMilliseconds() - now}ms`); | |
}); | |
} | |
const onDelete1Press = () => { | |
DB.foo.destroy(); | |
} | |
const onInsert2Press = () => { | |
const now = new Date().getMilliseconds(); | |
database.transaction(tx => { | |
let values = []; | |
for (let i = 0; i < 10000; i++) { | |
values.push(`("${Math.random().toString(32).substring(2)}","${Math.random().toString(32).substring(2)}",${new Date().getTime()})`) | |
} | |
tx.executeSql(`insert into players (foo,bar,created) values ${values.join(",")};`) | |
}, | |
() => { console.log('fail') }, | |
() => { console.log(`insert : ${new Date().getMilliseconds() - now}ms`) }, | |
); | |
} | |
const onAll2Press = async () => { | |
const now = new Date().getMilliseconds(); | |
database.transaction(tx => { | |
tx.executeSql( | |
'select * from players', | |
null, | |
(_, { rows: { _array } }) => { console.log(_array.length) } | |
) | |
}, | |
() => { console.log('fail') }, | |
() => { console.log(`all : ${new Date().getMilliseconds() - now}ms`) } | |
) | |
} | |
const onFind2Press = () => { | |
const now = new Date().getMilliseconds(); | |
database.transaction(tx => { | |
tx.executeSql( | |
'select * from players where id > 30 and bar LIKE "%a"', | |
null, | |
(_, { rows: { _array } }) => { console.log(_array.length) } | |
) | |
}, | |
() => { console.log('fail') }, | |
() => { console.log(`find : ${new Date().getMilliseconds() - now}ms`) } | |
) | |
} | |
const onDelete2Press = () => { | |
database.transaction(tx => { | |
tx.executeSql( | |
'delete from players', | |
) | |
}, | |
() => { console.log('fail') }, | |
() => { console.log(`delete`) } | |
) | |
} | |
const onInsert3Press = () => { | |
const now = new Date().getMilliseconds(); | |
const databaseLayer = new DatabaseLayer(async () => SQLite.openDatabase('database.db'), 'animals') | |
let data = [] | |
for (let i = 0; i < 10000; i++) { | |
data.push({ | |
foo: Math.random().toString(32).substring(2), | |
bar: Math.random().toString(32).substring(2), | |
}) | |
} | |
databaseLayer.bulkInsertOrReplace(data).then(response => { | |
console.log(`insert : ${new Date().getMilliseconds() - now}ms`) | |
}) | |
} | |
const onAll3Press = async () => { | |
const now = new Date().getMilliseconds(); | |
const res = await Animal.query({ | |
columns: "*" | |
}); | |
console.log(`all ${res.length} : ${new Date().getMilliseconds() - now}ms`) | |
} | |
const onFind3Press = async () => { | |
const now = new Date().getMilliseconds(); | |
const res = await Animal.query({ | |
columns: "*", | |
where: { | |
id_lt: 30, | |
bar_cont: "%a" | |
} | |
}); | |
console.log(`find ${res.length} : ${new Date().getMilliseconds() - now}ms`) | |
} | |
const onDelete3Press = () => { | |
Animal.destroyAll(); | |
} | |
return ( | |
<View style={styles.container}> | |
<Text>Open up App.js to start working on your app!</Text> | |
<TouchableOpacity style={{ backgroundColor: "#efefef", padding: 40 }} onPress={onInsert3Press}> | |
<Text>insert</Text> | |
</TouchableOpacity> | |
<TouchableOpacity style={{ backgroundColor: "#efefef", padding: 40 }} onPress={onAll3Press}> | |
<Text>all</Text> | |
</TouchableOpacity> | |
<TouchableOpacity style={{ backgroundColor: "#efefef", padding: 40 }} onPress={onFind3Press}> | |
<Text>find</Text> | |
</TouchableOpacity> | |
<TouchableOpacity style={{ backgroundColor: "#efefef", padding: 40 }} onPress={onDelete3Press}> | |
<Text>delete</Text> | |
</TouchableOpacity> | |
</View> | |
); | |
} | |
const styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
backgroundColor: '#fff', | |
alignItems: 'center', | |
justifyContent: 'center', | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment