Skip to content

Commit 9ba277b

Browse files
authored
chore(PouchDB-find): loop updates (#8918)
* chore: loop updates * chore: improve flatten performance
1 parent 3aa44a0 commit 9ba277b

File tree

4 files changed

+38
-53
lines changed

4 files changed

+38
-53
lines changed

packages/node_modules/pouchdb-find/src/adapters/local/find/query-planner.js

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,8 @@ const SHORT_CIRCUIT_QUERY = {
3131
// couchdb second-lowest collation value
3232

3333
function checkFieldInIndex(index, field) {
34-
const indexFields = index.def.fields.map(getKey);
35-
for (let i = 0, len = indexFields.length; i < len; i++) {
36-
const indexField = indexFields[i];
37-
if (field === indexField) {
38-
return true;
39-
}
40-
}
41-
return false;
34+
return index.def.fields
35+
.some((key) => getKey(key) === field);
4236
}
4337

4438
// so when you do e.g. $eq/$eq, we can do it entirely in the database.
@@ -92,14 +86,13 @@ function getBasicInMemoryFields(index, selector, userFields) {
9286

9387
function getInMemoryFieldsFromNe(selector) {
9488
const fields = [];
95-
Object.keys(selector).forEach(function (field) {
96-
const matcher = selector[field];
97-
Object.keys(matcher).forEach(function (operator) {
89+
for (const [field, matcher] of Object.entries(selector)) {
90+
for (const operator of Object.keys(matcher)) {
9891
if (operator === '$ne') {
9992
fields.push(field);
10093
}
101-
});
102-
});
94+
}
95+
}
10396
return fields;
10497
}
10598

@@ -212,8 +205,7 @@ function findBestMatchingIndex(selector, userFields, sortOrder, indexes, useInde
212205
function scoreIndex(index) {
213206
const indexFields = index.def.fields.map(getKey);
214207
let score = 0;
215-
for (let i = 0, len = indexFields.length; i < len; i++) {
216-
const indexField = indexFields[i];
208+
for (const indexField of indexFields) {
217209
if (userFieldsMap[indexField]) {
218210
score++;
219211
}
@@ -285,8 +277,7 @@ function getSingleFieldCoreQueryPlan(selector, index) {
285277

286278
let combinedOpts;
287279

288-
userOperators.forEach(function (userOperator) {
289-
280+
for (const userOperator of userOperators) {
290281
if (isNonLogicalMatcher(userOperator)) {
291282
inMemoryFields.push(field);
292283
}
@@ -300,7 +291,7 @@ function getSingleFieldCoreQueryPlan(selector, index) {
300291
} else {
301292
combinedOpts = newQueryOpts;
302293
}
303-
});
294+
}
304295

305296
return {
306297
queryOpts: combinedOpts,
@@ -389,8 +380,7 @@ function getMultiFieldQueryOpts(selector, index) {
389380

390381
let combinedOpts = null;
391382

392-
for (let j = 0; j < userOperators.length; j++) {
393-
const userOperator = userOperators[j];
383+
for (const userOperator of userOperators) {
394384
const userValue = matcher[userOperator];
395385

396386
const newOpts = getMultiFieldCoreQueryPlan(userOperator, userValue);

packages/node_modules/pouchdb-find/src/adapters/local/utils.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,18 @@ function massageIndexDef(indexDef) {
4949
}
5050

5151
function getKeyFromDoc(doc, index) {
52-
const res = [];
53-
for (let i = 0; i < index.def.fields.length; i++) {
54-
const field = getKey(index.def.fields[i]);
55-
res.push(getFieldFromDoc(doc, parseField(field)));
56-
}
57-
return res;
52+
return index.def.fields.map((obj) => {
53+
const field = getKey(obj);
54+
return getFieldFromDoc(doc, parseField(field));
55+
});
5856
}
5957

6058
// have to do this manually because REASONS. I don't know why
6159
// CouchDB didn't implement inclusive_start
6260
function filterInclusiveStart(rows, targetValue, index) {
6361
const indexFields = index.def.fields;
64-
let i = 0;
65-
for (let len = rows.length; i < len; i++) {
66-
const row = rows[i];
67-
62+
let startAt = 0;
63+
for (const row of rows) {
6864
// shave off any docs at the beginning that are <= the
6965
// target value
7066

@@ -83,8 +79,9 @@ function filterInclusiveStart(rows, targetValue, index) {
8379
// no need to filter any further; we're past the key
8480
break;
8581
}
82+
++startAt;
8683
}
87-
return i > 0 ? rows.slice(i) : rows;
84+
return startAt > 0 ? rows.slice(startAt) : rows;
8885
}
8986

9087
function reverseOptions(opts) {

packages/node_modules/pouchdb-find/src/massageCreateIndexRequest.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ function massageCreateIndexRequest(requestDef) {
1111
requestDef.index = {};
1212
}
1313

14-
['type', 'name', 'ddoc'].forEach(function (key) {
14+
for (const key of ['type', 'name', 'ddoc']) {
1515
if (requestDef.index[key]) {
1616
requestDef[key] = requestDef.index[key];
1717
delete requestDef.index[key];
1818
}
19-
});
19+
}
2020

2121
if (requestDef.fields) {
2222
requestDef.index.fields = requestDef.fields;

packages/node_modules/pouchdb-find/src/utils.js

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,23 +86,28 @@ function promisedCallback(promise, callback) {
8686
return promise;
8787
}
8888

89-
const flatten = function (...args) {
89+
const nativeFlat = (...args) => args.flat(Infinity);
90+
91+
const polyFlat = (...args) => {
9092
let res = [];
91-
for (let i = 0, len = args.length; i < len; i++) {
92-
const subArr = args[i];
93+
for (const subArr of args) {
9394
if (Array.isArray(subArr)) {
94-
res = res.concat(flatten.apply(null, subArr));
95+
res = res.concat(polyFlat(...subArr));
9596
} else {
9697
res.push(subArr);
9798
}
9899
}
99100
return res;
100101
};
101102

103+
const flatten = typeof Array.prototype.flat === 'function'
104+
? nativeFlat
105+
: polyFlat;
106+
102107
function mergeObjects(arr) {
103-
let res = {};
104-
for (let i = 0, len = arr.length; i < len; i++) {
105-
res = Object.assign(res, arr[i]);
108+
const res = {};
109+
for (const element of arr) {
110+
Object.assign(res, element);
106111
}
107112
return res;
108113
}
@@ -111,8 +116,8 @@ function mergeObjects(arr) {
111116
// and copies them to a new doc. Like underscore _.pick but supports nesting.
112117
function pick(obj, arr) {
113118
const res = {};
114-
for (let i = 0, len = arr.length; i < len; i++) {
115-
const parsedField = parseField(arr[i]);
119+
for (const field of arr) {
120+
const parsedField = parseField(field);
116121
const value = getFieldFromDoc(obj, parsedField);
117122
if (typeof value !== 'undefined') {
118123
setFieldInDoc(res, parsedField, value);
@@ -163,17 +168,16 @@ function oneSetIsSubArrayOfOther(left, right) {
163168

164169
function arrayToObject(arr) {
165170
const res = {};
166-
for (let i = 0, len = arr.length; i < len; i++) {
167-
res[arr[i]] = true;
171+
for (const field of arr) {
172+
res[field] = true;
168173
}
169174
return res;
170175
}
171176

172177
function max(arr, fun) {
173178
let max = null;
174179
let maxScore = -1;
175-
for (let i = 0, len = arr.length; i < len; i++) {
176-
const element = arr[i];
180+
for (const element of arr) {
177181
const score = fun(element);
178182
if (score > maxScore) {
179183
maxScore = score;
@@ -196,13 +200,7 @@ function arrayEquals(arr1, arr2) {
196200
}
197201

198202
function uniq(arr) {
199-
const obj = {};
200-
for (let i = 0; i < arr.length; i++) {
201-
obj['$' + arr[i]] = true;
202-
}
203-
return Object.keys(obj).map(function (key) {
204-
return key.substring(1);
205-
});
203+
return Array.from(new Set(arr));
206204
}
207205

208206
export {

0 commit comments

Comments
 (0)