fake-indexeddb 3.0.2 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/build/lib/RecordStore.js +14 -18
- package/build/lib/binarySearch.js +82 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
# 3.1.0 (2020-07-02)
|
|
2
|
+
|
|
3
|
+
- #52 - Significant performance improvement. 5.5x faster on a real use case. Thank you @nolanlawson for this speed up!
|
|
4
|
+
|
|
1
5
|
# 3.0.2 (2020-06-10)
|
|
2
6
|
|
|
3
7
|
* #45 - Fix synchronous event firing in a transaction, which led to a stack overflow when used with Dexie's waitFor function.
|
package/build/lib/RecordStore.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
var FDBKeyRange_1 = require("../FDBKeyRange");
|
|
4
|
+
var binarySearch_1 = require("./binarySearch");
|
|
4
5
|
var cmp_1 = require("./cmp");
|
|
5
6
|
var RecordStore = /** @class */ (function () {
|
|
6
7
|
function RecordStore() {
|
|
@@ -8,13 +9,9 @@ var RecordStore = /** @class */ (function () {
|
|
|
8
9
|
}
|
|
9
10
|
RecordStore.prototype.get = function (key) {
|
|
10
11
|
if (key instanceof FDBKeyRange_1.default) {
|
|
11
|
-
return this.records
|
|
12
|
-
return key.includes(record.key);
|
|
13
|
-
});
|
|
12
|
+
return binarySearch_1.getByKeyRange(this.records, key);
|
|
14
13
|
}
|
|
15
|
-
return this.records
|
|
16
|
-
return cmp_1.default(record.key, key) === 0;
|
|
17
|
-
});
|
|
14
|
+
return binarySearch_1.getByKey(this.records, key);
|
|
18
15
|
};
|
|
19
16
|
RecordStore.prototype.add = function (newRecord) {
|
|
20
17
|
// Find where to put it so it's sorted by key
|
|
@@ -23,11 +20,7 @@ var RecordStore = /** @class */ (function () {
|
|
|
23
20
|
i = 0;
|
|
24
21
|
}
|
|
25
22
|
else {
|
|
26
|
-
i = this.records.
|
|
27
|
-
// cmp will only return 0 for an index. For an object store, any matching key has already been deleted,
|
|
28
|
-
// but we still need to look for cmp = 1 to find where to insert.
|
|
29
|
-
return cmp_1.default(record.key, newRecord.key) >= 0;
|
|
30
|
-
});
|
|
23
|
+
i = binarySearch_1.getIndexByKeyGTE(this.records, newRecord.key);
|
|
31
24
|
if (i === -1) {
|
|
32
25
|
// If no matching key, add to end
|
|
33
26
|
i = this.records.length;
|
|
@@ -47,15 +40,18 @@ var RecordStore = /** @class */ (function () {
|
|
|
47
40
|
this.records.splice(i, 0, newRecord);
|
|
48
41
|
};
|
|
49
42
|
RecordStore.prototype.delete = function (key) {
|
|
50
|
-
var range = key instanceof FDBKeyRange_1.default ? key : FDBKeyRange_1.default.only(key);
|
|
51
43
|
var deletedRecords = [];
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
44
|
+
var isRange = key instanceof FDBKeyRange_1.default;
|
|
45
|
+
while (true) {
|
|
46
|
+
var idx = isRange
|
|
47
|
+
? binarySearch_1.getIndexByKeyRange(this.records, key)
|
|
48
|
+
: binarySearch_1.getIndexByKey(this.records, key);
|
|
49
|
+
if (idx === -1) {
|
|
50
|
+
break;
|
|
56
51
|
}
|
|
57
|
-
|
|
58
|
-
|
|
52
|
+
deletedRecords.push(this.records[idx]);
|
|
53
|
+
this.records.splice(idx, 1);
|
|
54
|
+
}
|
|
59
55
|
return deletedRecords;
|
|
60
56
|
};
|
|
61
57
|
RecordStore.prototype.deleteByValue = function (key) {
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var cmp_1 = require("./cmp");
|
|
4
|
+
/**
|
|
5
|
+
* Classic binary search implementation. Returns the index where the key
|
|
6
|
+
* should be inserted, assuming the records list is ordered.
|
|
7
|
+
*/
|
|
8
|
+
function binarySearch(records, key) {
|
|
9
|
+
var low = 0;
|
|
10
|
+
var high = records.length;
|
|
11
|
+
var mid;
|
|
12
|
+
while (low < high) {
|
|
13
|
+
// tslint:disable-next-line:no-bitwise
|
|
14
|
+
mid = (low + high) >>> 1; // like Math.floor((low + high) / 2) but fast
|
|
15
|
+
if (cmp_1.default(records[mid].key, key) < 0) {
|
|
16
|
+
low = mid + 1;
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
high = mid;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return low;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Equivalent to `records.findIndex(record => cmp(record.key, key) === 0)`
|
|
26
|
+
*/
|
|
27
|
+
function getIndexByKey(records, key) {
|
|
28
|
+
var idx = binarySearch(records, key);
|
|
29
|
+
var record = records[idx];
|
|
30
|
+
if (record && cmp_1.default(record.key, key) === 0) {
|
|
31
|
+
return idx;
|
|
32
|
+
}
|
|
33
|
+
return -1;
|
|
34
|
+
}
|
|
35
|
+
exports.getIndexByKey = getIndexByKey;
|
|
36
|
+
/**
|
|
37
|
+
* Equivalent to `records.find(record => cmp(record.key, key) === 0)`
|
|
38
|
+
*/
|
|
39
|
+
function getByKey(records, key) {
|
|
40
|
+
var idx = getIndexByKey(records, key);
|
|
41
|
+
return records[idx];
|
|
42
|
+
}
|
|
43
|
+
exports.getByKey = getByKey;
|
|
44
|
+
/**
|
|
45
|
+
* Equivalent to `records.findIndex(record => key.includes(record.key))`
|
|
46
|
+
*/
|
|
47
|
+
function getIndexByKeyRange(records, keyRange) {
|
|
48
|
+
var lowerIdx = typeof keyRange.lower === "undefined"
|
|
49
|
+
? 0
|
|
50
|
+
: binarySearch(records, keyRange.lower);
|
|
51
|
+
var upperIdx = typeof keyRange.upper === "undefined"
|
|
52
|
+
? records.length - 1
|
|
53
|
+
: binarySearch(records, keyRange.upper);
|
|
54
|
+
for (var i = lowerIdx; i <= upperIdx; i++) {
|
|
55
|
+
var record = records[i];
|
|
56
|
+
if (keyRange.includes(record.key)) {
|
|
57
|
+
return i;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return -1;
|
|
61
|
+
}
|
|
62
|
+
exports.getIndexByKeyRange = getIndexByKeyRange;
|
|
63
|
+
/**
|
|
64
|
+
* Equivalent to `records.find(record => key.includes(record.key))`
|
|
65
|
+
*/
|
|
66
|
+
function getByKeyRange(records, keyRange) {
|
|
67
|
+
var idx = getIndexByKeyRange(records, keyRange);
|
|
68
|
+
return records[idx];
|
|
69
|
+
}
|
|
70
|
+
exports.getByKeyRange = getByKeyRange;
|
|
71
|
+
/**
|
|
72
|
+
* Equivalent to `records.findIndex(record => cmp(record.key, key) >= 0)`
|
|
73
|
+
*/
|
|
74
|
+
function getIndexByKeyGTE(records, key) {
|
|
75
|
+
var idx = binarySearch(records, key);
|
|
76
|
+
var record = records[idx];
|
|
77
|
+
if (record && cmp_1.default(record.key, key) >= 0) {
|
|
78
|
+
return idx;
|
|
79
|
+
}
|
|
80
|
+
return -1;
|
|
81
|
+
}
|
|
82
|
+
exports.getIndexByKeyGTE = getIndexByKeyGTE;
|
package/package.json
CHANGED