Couchbaselite swift (ios) upsert is too slow

I’m adding aprox 1 Million records in database. I’m doing upsert (code snippet below).
It is processing 80-100 records per second and this number reduces as data grows in DB.

SDK version

pod 'CouchbaseLite-Swift', '3.1.1'

Please let me know if I’m doing anything wrong here.

private func upsertObjects(_ objects: [[String: Any]], inTable table: String, skipDirty: Bool = true, replace: Bool = true) -> Bool {

        do {
            ADPLog.Debug("start batch")
            try database.inBatch {
                for object in objects {
                    guard let objectId = object["id"] as? String else {
                        continue
                    }
                    let existingDoc = try database.defaultCollection().document(id: objectId)?.toDictionary()
                    if skipDirty, let existingDoc, isDirty(json: existingDoc) {
                        continue
                    }
                    var updatedJSON = object
                    if replace, let existingDoc {
                        updatedJSON = mergeJSON(existingDoc, updatedJSON)
                    }
                    let mutableDoc = MutableDocument(id: objectId, data: updatedJSON)
                    mutableDoc.setValue(table, forKey: DOCTYPE)
                    try database.defaultCollection().save(document: mutableDoc)
                }
            }
            ADPLog.Debug("end batch")
            return true
        }
        catch {
            ADPLog.Error(error.localizedDescription)
            return false
        }
    }

    private func isDirty(json: [String: Any]) -> Bool {
        guard let dirtyFlag = json[IS_DIRTY] as? Bool, dirtyFlag == true else {
            return false
        }
        return true
    }