Skip to content

Commit

Permalink
Initial refactoring to storing ArrayBuffer instances instead of `Ob…
Browse files Browse the repository at this point in the history
…jects`
  • Loading branch information
avoidwork committed Dec 3, 2020
1 parent 792cd3b commit c98d3be
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
.eslintrc
.gitignore
.travis.yml
rollup.config.js
data.json
rollup.config.js
94 changes: 94 additions & 0 deletions benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
const path = require("path"),
{haro} = require(path.join(__dirname, "dist", "haro.cjs.js")),
precise = require("precise"),
data = require(path.join(__dirname, "data.json"));

let indexes;

function second () {
const timer = precise().start(),
store = haro(null, {id: "test", key: "_id", index: ["name", "eyeColor", "age", "gender", "isActive"]}),
deferreds = [];

deferreds.push(store.override(data, "records"));
deferreds.push(store.override(indexes, "indexes"));

Promise.all(deferreds).then(function () {
let i = -1,
nth = 5;

timer.stop();
console.log("time to override data: " + (timer.diff() / 1000000) + "ms");
console.log("testing time to 'search(regex, index)' on overridden data for a record (first one is cold):");

while (++i < nth) {
(function () {
const timer2 = precise().start();
const record = store.search(/Carly Conway/, "name");
timer2.stop();
console.log((timer2.diff() / 1000000) + "ms");

if (!record) {
console.log("Couldn't find record");
}
}());
}
});
}

function first () {
const timer = precise().start(),
store = haro(null, {id: "test", key: "_id", index: ["name", "eyeColor", "age", "gender", "isActive"]});

store.batch(data, "set").then(function () {
timer.stop();
console.log("time to batch insert data: " + (timer.diff() / 1000000) + "ms");
console.log("datastore record count: " + store.total);
console.log("name indexes: " + store.indexes.get("name").size + "\n");
}).then(function () {
let i = -1,
nth = 5;

console.log("testing time to 'find()' a record (first one is cold):");
indexes = store.dump("indexes");

while (++i < nth) {
(function () {
const timer2 = precise().start();
const record = store.find({name: "Muriel Osborne"});
timer2.stop();
console.log((timer2.diff() / 1000000) + "ms");

if (!record) {
console.log("Couldn't find record");
}
}());
}

console.log("");
}).then(function () {
let i = -1,
nth = 5;

console.log("testing time to 'search(regex, index)' for a record (first one is cold):");

while (++i < nth) {
(function () {
const timer2 = precise().start();
const record = store.search(/Lizzie Clayton/, "name");
timer2.stop();
console.log((timer2.diff() / 1000000) + "ms");

if (!record) {
console.log("Couldn't find record");
}
}());
}

console.log("");

second();
});
}

first();
25 changes: 21 additions & 4 deletions dist/haro.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ Object.defineProperty(exports, '__esModule', { value: true });

const r = [8, 9, "a", "b"];

function decode (arg = new ArrayBuffer(0)) {
return JSON.parse(String.fromCharCode.apply(null, new Uint16Array(arg)));
}

function encode (arg = "") {
const str = JSON.stringify(arg),
buf = new ArrayBuffer(str.length * 2), // 2 bytes for each char
bufView = new Uint16Array(buf);
let i = -1;

for (const char of str) {
bufView[++i] = char.charCodeAt(0);
}

return buf;
}

function clone (arg) {
return JSON.parse(JSON.stringify(arg, null, 0));
}
Expand Down Expand Up @@ -232,13 +249,13 @@ class Haro {
}

forEach (fn, ctx) {
this.data.forEach((value, key) => fn(clone(value), clone(key)), ctx || this.data);
this.data.forEach((value, key) => fn(decode(value), clone(key)), ctx || this.data);

return this;
}

get (key, raw = false) {
const result = clone(this.data.get(key) || null);
const result = this.has(key) ? decode(this.data.get(key)) : null;

return raw ? result : this.list(key, result);
}
Expand Down Expand Up @@ -292,7 +309,7 @@ class Haro {
this.indexes = new Map(data.map(i => [i[0], new Map(i[1].map(ii => [ii[0], new Set(ii[1])]))]));
} else if (type === "records") {
this.indexes.clear();
this.data = new Map(data);
this.data = new Map(data.map(i => [i[0], encode(i[1])]));
this.size = this.data.size;
} else {
throw new Error("Invalid type");
Expand Down Expand Up @@ -383,7 +400,7 @@ class Haro {
}
}

this.data.set(key, x);
this.data.set(key, encode(x));
setIndex(this.index, this.indexes, this.delimiter, key, x, null);
result = this.get(key);
this.onset(result, batch, retry, lazyLoad);
Expand Down
2 changes: 1 addition & 1 deletion dist/haro.esm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c98d3be

Please sign in to comment.