Like it or hate it, the capabilities of your modern web browser continue to grow in weird and wild ways. The ability of web applications to work offline requires a persistent local storage solution. For many people, IndexedDB is the only option because it can work across most browsers and provide a database-like interface.However, as [James Long] It is found that IndexedDB is very slow on chrome and has limited query capabilities.He set out to bring a familiar tool, SQLite, and Bring it to the web browser as absurd-sql.
Why is it absurd? Partly because most browsers (not chrome) implement IndexedDB on top of SQLite. Therefore, for many browsers, it is just SQLite on top of IndexedDB on top of SQLite.Fortunately [James] There is already a project called sql.js use script Compile C-based SQLite into WebAssembly. However, sql.js uses memory storage support, and all data will be lost when the page is refreshed. [James] Adjusted the way SQLite reads and writes blocks. Instead of using memory support, he added a layer to read and write blocks from IndexedDB. This means that only part of the database needs to be read, resulting in a huge performance improvement.
This brings us to another absurd reason. On Chrome (and Firefox), absurd-sql beats IndexedDB in almost all benchmarks.Query like this SELECT SUM(*) FROM kv
Led to amazing results.
So what are the disadvantages? Except for the larger WebAssembly files that need to be downloaded (409KB) and cached, there really are none. Of course, this is not all true when it comes to Web development. The running speed of native SQLite is 2-3 times faster than absurd-sql, which shows the real speed of IndexedDB.
There are other Upcoming storage standards For web browsers, locking becomes a problem. SQLite expects synchronous reads and writes because it is just plain C. IndexedDB and other storage solutions are asynchronous because Javascript’s event loop fits this model very well. Absurd-sql solves this problem by creating a SharedArrayBuffer shared with the worker process. The atomic API is used to communicate with the buffer. especially, atomics.wait()
Allow the worker thread to block the main thread execution until the read or write is complete. From the perspective of SQLite, operations are synchronous. IndexedDB provides transactions, so multiple connections can occur (such as opening multiple tabs). Multiple read-only transactions can occur in parallel, but only one read-write transaction can occur.
Why not open your browser and start playing with it? In any case, you are destined to learn WebAssembly.