This project builds LevelDb as a Wasm library to give sites direct access to a performant device-local key/value store.
Emscripten is used to compile C++ to Web Assembly.
The data is stored via the site's Origin Private File System.
The project should be considered a POC or example of how to build a Wasm library.
- A limited set of LevelDb operations are thus far provided (e.g. no
WriteOptions
). - Few performance optimizations have been pursued.
- Test coverage is minimal.
$ sudo apt install cmake
$ git clone --recurse-submodules git@github.com:a-sully/leveldb.git
$ cd leveldb
$ cd third_party/emsdk
$ ./emsdk install latest
$ ./emsdk activate latest
$ source ./emsdk_env.sh
$ cd ../..
$ mkdir -p build && cd build
$ emcmake cmake -DCMAKE_BUILD_TYPE=Release ..
$ cmake --build . --target leveldbwasmdemo
Tip: set
CMAKE_BUILD_PARALLEL_LEVEL
or use-jN
to build faster.
Using threads in WASM requires SharedArrayBuffer, which is only enabled when a page is cross-origin isolated, which requires adding the following headers to all HTTP responses:
Cross-Origin-Embedder-Policy: require-corp
Cross-Origin-Opener-Policy: same-origin
The usual local development servers such as the one built into Python don’t send these headers. One built with Rust however, called sfz provides a --coi option to enable the necessary headers.
Build the leveldbwasmdemo
target and direct your browser to demo.html
on your local server, e.g. http://localhost:5000/demo.html
.
Build the leveldbwasmbench
target and direct your browser to benchmark.html
on your local server, e.g. http://localhost:5000/benchmark.html
.
This compares the speed of LevelDB via Wasm to IndexedDB (wrapped with IDB-Keyval) for some simple key/value store operations, mainly put
, get
and delete
. DevTools should be able to show additional metrics such as local storage space used (in Chromium this is found under the Application
tab).
Preliminary results indicate a 25-35% speed-up for fetching data, with similar write speed and storage footprint.
The public-facing API is published in ./wasm/js-api/
. Some code for interfacing between ES and C++ resides in ./wasm/idl/
. For usage examples, refer to the demo and benchmark apps
- Reilly Grant (reillyg@google.com)
- Nathan Memmot (memmott@google.com)
- Evan Stade (estade@google.com)
- Austin Sullivan (asully@google.com)