Skip to content

Commit 20ed5e9

Browse files
authored
perf: fix performance for cases with sparsely connected entities (#278)
1 parent 2fdd465 commit 20ed5e9

2 files changed

Lines changed: 90 additions & 3 deletions

File tree

perf/N3Store-perf.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,92 @@ for (k = 0; k < dim; k++)
8585
for (l = 0; l < dim; l++)
8686
assert.equal(store.getQuads(null, null, null, prefix + l).length, dimCubed);
8787
console.timeEnd(TEST);
88+
89+
console.log('N3 Store tests for sparsely connected entities');
90+
91+
store = new N3.Store();
92+
TEST = `- Adding ${dimQuads} with all different IRIs`;
93+
console.time(TEST);
94+
for (let i = 0; i < dimQuads; i++) {
95+
store.addQuad(
96+
prefix + i,
97+
prefix + i,
98+
prefix + i
99+
);
100+
}
101+
console.timeEnd(TEST);
102+
103+
104+
TEST = `* Retrieving all ${dimQuads} quads`;
105+
console.time(TEST);
106+
for (const quad of store.match(undefined, undefined, undefined)) {
107+
assert(quad);
108+
}
109+
console.timeEnd(TEST);
110+
111+
TEST = '* Retrieving single by subject';
112+
console.time(TEST);
113+
for (let i = 0; i < 1000000; i++) {
114+
for (const quad of store.match(prefix + 1, undefined, undefined)) {
115+
assert(quad);
116+
}
117+
}
118+
console.timeEnd(TEST);
119+
120+
121+
TEST = '* Retrieving single by predicate';
122+
console.time(TEST);
123+
for (let i = 0; i < 1000000; i++) {
124+
for (const quad of store.match(undefined, prefix + 1, undefined)) {
125+
assert(quad);
126+
}
127+
}
128+
console.timeEnd(TEST);
129+
130+
TEST = '* Retrieving single by object';
131+
console.time(TEST);
132+
for (let i = 0; i < 1000000; i++) {
133+
for (const quad of store.match(undefined, undefined, prefix + 1)) {
134+
assert(quad);
135+
}
136+
}
137+
console.timeEnd(TEST);
138+
139+
140+
TEST = '* Retrieving single by subject-predicate';
141+
console.time(TEST);
142+
for (let i = 0; i < 1000000; i++) {
143+
for (const quad of store.match(prefix + 1, prefix + 1, undefined)) {
144+
assert(quad);
145+
}
146+
}
147+
console.timeEnd(TEST);
148+
149+
150+
TEST = '* Retrieving single by subject-object';
151+
console.time(TEST);
152+
for (let i = 0; i < 1000000; i++) {
153+
for (const quad of store.match(prefix + 1, undefined, prefix + 1)) {
154+
assert(quad);
155+
}
156+
}
157+
console.timeEnd(TEST);
158+
159+
TEST = '* Retrieving single by predicate-object';
160+
console.time(TEST);
161+
for (let i = 0; i < 1000000; i++) {
162+
for (const quad of store.match(undefined, prefix + 1, prefix + 1)) {
163+
assert(quad);
164+
}
165+
}
166+
console.timeEnd(TEST);
167+
168+
169+
TEST = '* Retrieving single by subject-predicate-object';
170+
console.time(TEST);
171+
for (let i = 0; i < 1000000; i++) {
172+
for (const quad of store.match(prefix + 1, prefix + 1, prefix + 1)) {
173+
assert(quad);
174+
}
175+
}
176+
console.timeEnd(TEST);

src/N3Store.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,7 @@ export default class N3Store {
8787
// Finally, `graphId` will be the graph of the created quads.
8888
*_findInIndex(index0, key0, key1, key2, name0, name1, name2, graphId) {
8989
let tmp, index1, index2;
90-
// Depending on the number of variables, keys or reverse index are faster
91-
const varCount = !key0 + !key1 + !key2,
92-
entityKeys = varCount > 1 ? Object.keys(this._ids) : this._entities;
90+
const entityKeys = this._entities;
9391
const graph = termFromId(graphId, this._factory);
9492
const parts = { subject: null, predicate: null, object: null };
9593

0 commit comments

Comments
 (0)