Dexie.js performance
Dexie.js performance
HTML
<!-- Include dexie.js -->
<script src="https://unpkg.com/dexie@^2.0.0-beta/dist/dexie.js"></script>
<!-- Some HTML for the log window -->
<a href="http://dexie.org/docs/API-Reference" target="_new">Dexie API Reference (new tab)</a>
<div style="float:right">Check out this
<a href="https://jsfiddle.net/dfahlander/5rbc996o/" target="yf">
await fiddle </a> also!
</div>
<h3>Log</h3>
<textarea id="log"></textarea>
<!-- Just a simple log function... -->
<script>
function log(txt) {document.getElementById('log').value+=txt+"\n";}
</script>
CSS
textarea#log
{
width: 100%;
height: 1000px;
}
JavaScript
//
// Define database
//
var db = new Dexie("raindrops");
db.version(1).stores({
raindrops: 'id,position',
});
log ("Using Dexie v" + Dexie.semVer);
var time = 0;
var drops = [];
// Create data to put later on.
for (var i=0;i<10000;++i) {
drops.push({
id: i,
position: Math.random(),
someData: {
someText: "some value",
someNumber: Math.random()
}
});
}
//
// Open Database
//
db.open().then(function(){
log("Let's put a 10,000 documents into indexedDB! ...");
return Promise.resolve(); // Give GUI some breath to show the message
}).then (function () {
time = Date.now();
return db.raindrops.bulkPut(drops);
}).then(function(lastKey) {
log("Done putting 10,000 documents all over the place");
log("Last document's id was: " + lastKey); // Will be 9999.
}).catch(Dexie.BulkError, function (e) {
// Explicitely catching the bulkPut() operation makes those successful
// additions commit despite that there were errors.
log ("Some documents did not succeed. However, " +
10000-e.failures.length + " documents was added successfully");
}).then(function(){
log("Finished. Took " + (Date.now() - time) + " milliseconds.");
}).catch ('MissingAPIError', function (e) {
log ("Couldn't find indexedDB API");
}).catch ('SecurityError', function(e) {
log ("SeurityError - This browser doesn't like fiddling with indexedDB.");
log ("If using Safari, this is because jsfiddle runs its samples within an iframe");
log ("Go run some samples instead at: https://github.com/dfahlander/Dexie.js/wiki/Samples");
}).catch (function (e) {
log (e);
});