Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News LokiJS 1.4 Release Brings Performance Improvement, NativeScript Adapter

LokiJS 1.4 Release Brings Performance Improvement, NativeScript Adapter

This item in japanese

Version 1.4 of LokiJS, the in-memory JavaScript database that prioritises performance, has been released.

Named after Norse mythology's trickster god, Loki supports collections in a similar way to MongoDB, supporting field indexing for faster document access, and saves data to disk in JSON, making data portable.

Talking to InfoQ, the project's self described "Mis-Chief" developer Joe Minichino, says the major release fixes "a number of bugs, and brings a big performance improvement."

Minichino said:

Some query operators like $where, $ne, $len, $keyin, $nkeyin, $type, $contains and $containsAny were added or improved. Also very handy is the time-to-live (TTL) feature, which automatically deletes object that have not been accessed within a certain time interval. This is useful for storing sessions or in games, for example.

Loki originally started life as a solution for storing data on phonegap/cordova apps, but as Minichino rejected SQLite as "too cumbersome" for what he needed for Loki. If you're working in JavaScript, with JavaScript objects and object literals, translating them into tables can be a pain, Minichino says, so Loki created as a component to take objects and serialise to JSON on the local filesystem, so developers reload the data in following work sessions.

1.4 adds an adapter for NativeScript apps. The following code is given as an example:

// Requirements
var fs = require("file-system");
var Loki = require("./node_modules/lokijs/src/lokijs.js");
var LokiNativeScriptAdapter = require("./node_modules/loki-nativescript-adapter/loki-nativescript-adapter.js");

// Setup Loki
var path = fs.path.join(fs.knownFolders.currentApp().path, "database.db");
var db = new Loki(path, {
    adapter: new LokiNativeScriptAdapter()

// Save some movies
var movies = db.addCollection("movies");
movies.insert({ title: "Ghost Busters", year: 1984 });
movies.insert({ title: "Ghost Busters II", year: 1989 });
movies.insert({ title: "Ghost Busters", year: 2016 });

// Load and find some movies
db.loadDatabase({}, function() {
    var movies = db.getCollection("movies");
    console.log(movies.find({ title: "Ghost Busters" }));

The major release is described by Minichino as "the meticulous work" of three developers: obeliskos, aleandernst and VladimirTechMan who are collectively responsible for improving internals on queries and dynamic views, further boosting performance.

On the roadmap for LokiJS is a server-side persistence adapter. Currently this exists with the whole db serialised in one file, with the file being overwritten on save.

For developers using LokiJS for a db of a few GB of size, a single update to one record rewrites the file. While this is no issue in browser or on mobile when the size db is a few MB, because the save operation is in the order of milliseconds, for backends Minichino says incremental persistence adapters are required, with http/tcp wrappers for networking and replication.

LokiJS is released via an MIT licence. InfoQ readers enthusiastic to contribute to the project should sign up on GitHub and submit pull requests or open issues. The LokiJS team is keen to hear constructive criticism from anyone with negative experiences.

Rate this Article