In IndexedDB gibt es zwei Möglichkeiten, ein bereits in der Datenbank vorhandenes Objekt zu aktualisieren. Sie können IDBCursor.update
oder IDBObjectStore.put
anrufen.Was ist der Unterschied zwischen IDBObjectStore.put und IDBCursor.update in IndexedDB?
Beide akzeptieren das aktualisierte Objekt als Parameter.
IDBCursor.update
müssen Sie zuerst einen Cursor öffnen, aber Sie müssen das auch mit IDBObjectStore.put
tun, um den vorherigen Wert abzurufen.
IDBObjectStore.put
erstellt ein neues Objekt, wenn es nicht zu aktualisieren gibt, aber da es zuerst nach einem Update suchen muss, weiß ich nicht, ob dies tatsächlich einen Leistungsunterschied erzeugen würde.
Was ist der Unterschied zwischen diesen Methoden? Gibt es etwas, das ich vermisse? Ich habe versucht, einen Testfall, um Leistungsunterschiede zu untersuchen:
var db;
function runTest(N, cb) {
console.log("N = " + N);
// Add some fake data to object store
var tx = db.transaction("store", "readwrite");
tx.objectStore("store").clear();
for (var i = 0; i < N; i++) {
tx.objectStore("store").add({"id": i, "index":0,"guid":"21310c91-ff31-4cb9-ae68-16d48cbbd84a","isActive":false,"balance":"$1,840.25","picture":"http://placehold.it/32x32","age":33,"eyeColor":"brown","name":"Witt Fletcher","gender":"male","company":"QUILM","email":"[email protected]","phone":"+1 (851) 485-2174","address":"729 Conover Street, Marenisco, Virginia, 7219","about":"Incididunt do deserunt ut quis. Exercitation et ut ad aliqua ut do sint Lorem. Aliquip sit aliquip nulla excepteur pariatur ut laborum ea dolor. Consectetur incididunt et et esse commodo id eu dolor in. Nostrud sit mollit occaecat ullamco commodo aute anim duis enim et aliqua. Aute duis nostrud do minim labore sunt mollit in voluptate aliquip sit. Aliqua aliquip non ipsum exercitation cillum irure in.\r\n","registered":"2014-07-02T03:42:57 +04:00","latitude":-65.942119,"longitude":-129.471674,"tags":["reprehenderit","nostrud","velit","exercitation","nulla","nulla","est"],"friends":[{"id":0,"name":"Kristine Francis"},{"id":1,"name":"Lizzie Ruiz"},{"id":2,"name":"Bobbie Underwood"}],"greeting":"Hello, Witt Fletcher! You have 7 unread messages.","favoriteFruit":"apple"});
}
tx.oncomplete = function() {
// Update with cursor.update
var tStart = (new Date()).getTime();
tx = db.transaction("store", "readwrite");
var store = tx.objectStore("store");
for (var i = 0; i < N; i++) {
store.openCursor(i).onsuccess = function (event) {
var cursor = event.target.result;
cursor.value.age = 34;
cursor.update(cursor.value);
};
}
tx.oncomplete = function() {
var tEnd = (new Date()).getTime();
console.log("cursor.update - " + (tEnd - tStart) + " milliseconds");
// Update with put
tStart = (new Date()).getTime();
tx = db.transaction("store", "readwrite");
store = tx.objectStore("store");
for (var i = 0; i < N; i++) {
store.openCursor(i).onsuccess = function (event) {
var cursor = event.target.result;
cursor.value.age = 34;
store.put(cursor.value);
};
}
tx.oncomplete = function() {
tEnd = (new Date()).getTime();
console.log("put - " + (tEnd - tStart) + " milliseconds");
if (cb !== undefined) {
cb();
}
};
};
};
}
request = indexedDB.open("yes5ytrye", 1);
request.onerror = function (event) { console.log(event); };
request.onupgradeneeded = function (event) {
var db = event.target.result;
db.onerror = function (event) { console.log(event); };
db.createObjectStore("store", {keyPath: "id"});
};
request.onsuccess = function (event) {
db = request.result;
db.onerror = function (event) { console.log(event); };
runTest(100, function() {
runTest(1000, function() {
runTest(10000, function() {
console.log("Done");
});
});
});
};
Sie können versuchen, es here.
In Firefox, erhalte ich folgende Ausgabe:
N = 100
cursor.update - 39 milliseconds
put - 40 milliseconds
N = 1000
cursor.update - 229 milliseconds
put - 256 milliseconds
N = 10000
cursor.update - 2194 milliseconds
put - 2096 milliseconds
Done
im Grunde keinen Unterschied in der Leistung. Die Ergebnisse sind ein wenig anders in Chrome, wenn N
ist groß:
N = 100
cursor.update - 51 milliseconds
put - 44 milliseconds
N = 1000
cursor.update - 414 milliseconds
put - 447 milliseconds
N = 10000
cursor.update - 13506 milliseconds
put - 22783 milliseconds
Done
Aber wie ich schon oben gesagt, ich bin nicht einmal sicher, ob es sollte ein Unterschied zwischen diesen beiden Methoden, weil es scheint, wie sie müssen mache genau dasselbe.