My problem mainly is performance related, I have this code running on the main ElectronJS proccess :
ipcMain.handle('add_product', async (event, args)=>{
return new Promise((resolve, reject)=>{
try {
if(Array.isArray(args)){
args.forEach(prod =>{
const {name,barcode,stock,price,buy_price,image,alert} = prod
const stmt = db.prepare("INSERT INTO products VALUES (?,?,?,?,?,?,?)")
stmt.run(name, barcode, stock, alert, price, buy_price, image)
stmt.finalize()
})
resolve({text : `${args.length} product have been added to database!`})
}else{
// This code execute's only when adding a single product
// It is not relevant to the question
const {name,barcode,stock,price,buy_price,image,alert} = args
const stmt = db.prepare("INSERT INTO products VALUES (?,?,?,?,?,?,?)")
stmt.run(name, barcode, stock, alert, price, buy_price, image)
stmt.finalize()
resolve({text : `Product '${name}' have been saved!`})
}
}catch (error){
reject(error)
}
})
})
It receives an array of objects, each object contains a single product details. Now the above code works and successfully inserts rows inside the database. However when testing it with a substantial data sample (more than 5000 product) the whole application freezes for a couple of seconds while it is saving rows to the database before it becomes responsive again.
The dev stack is :
- ElectronJS
- ReactJS (using it for the VIEW)
- SQLite
What is the optimal and performance driven way to make the application works fatser?