Skip to content

Instantly share code, notes, and snippets.

@nicholasRutherford
Last active November 24, 2023 23:15
Show Gist options
  • Save nicholasRutherford/9af30ac7c97f4ce78bece03f4edf9e05 to your computer and use it in GitHub Desktop.
Save nicholasRutherford/9af30ac7c97f4ce78bece03f4edf9e05 to your computer and use it in GitHub Desktop.
lokijs vs js array
var loki = require('lokijs');
var BTree = require('sorted-btree').default
var baseRecordCount = 10000000;
var newRecordCount = 10000;
function generateRandomId() {
var max = 10000000;
return Math.floor(Math.random() * max) + 1;
}
function generateRandomData(count) {
var items = [];
for (var i = 0; i < count; i++) {
items.push({ id: generateRandomId() });
}
return items;
}
// Using lokijs
var baseData = generateRandomData(baseRecordCount);
var newData = generateRandomData(newRecordCount);
var db = new loki('Example', {
autosave: false, // Disable autosaving
autoload: false, // Disable automatic loading from a file
persistenceMethod: null // Ensure no persistence method is used
});
var markets = db.addCollection('markets');
console.log("Using lokijs");
console.time(`LokiJS Loading ${baseRecordCount}`);
markets.insert(baseData);
console.timeEnd(`LokiJS Loading ${baseRecordCount}`);
console.time(`LokiJS Sorting ${baseRecordCount}`);
markets.ensureIndex('id');
console.timeEnd(`LokiJS Sorting ${baseRecordCount}`);
console.time(`LokiJS Insert and Sort ${newRecordCount}`);
markets.insert(newData);
console.timeEnd(`LokiJS Insert and Sort ${newRecordCount}`);
// Using base js array
var baseData = generateRandomData(baseRecordCount);
var newData = generateRandomData(newRecordCount);
console.log("Using base js");
console.time(`JS Array Sorting ${baseRecordCount}`);
baseData.sort((a, b) => a.id - b.id);
console.timeEnd(`JS Array Sorting ${baseRecordCount}`);
console.time(`JS Insert and Sort ${newRecordCount}`);
baseData.push(...newData);
baseData.sort((a, b) => a.id - b.id);
console.timeEnd(`JS Insert and Sort ${newRecordCount}`);
// using btree
var baseData = generateRandomData(baseRecordCount);
var newData = generateRandomData(newRecordCount);
var tree = new BTree(undefined, (a, b) => a.id - b.id);
// Insert base data into B-tree and measure time
console.log("Using B-tree");
console.time(`B-tree Insertion ${baseRecordCount}`);
baseData.forEach(item => tree.set(item, item.id));
console.timeEnd(`B-tree Insertion ${baseRecordCount}`);
// Insert new data into B-tree and measure time
console.time(`B-tree Insert and Sort ${newRecordCount}`);
newData.forEach(item => tree.set(item, item.id));
console.timeEnd(`B-tree Insert and Sort ${newRecordCount}`);
// Using lokijs
// LokiJS Loading 10000000: 2.238s
// LokiJS Sorting 10000000: 7.816s
// LokiJS Insert and Sort 10000: 9.089s
// Using base js
// JS Array Sorting 10000000: 4.588s
// JS Insert and Sort 10000: 474.662ms
// Using B-tree
// B-tree Insertion 10000000: 12.956s
// B-tree Insert and Sort 10000: 15.891ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment