Skip to content

Commit 8d53f5d

Browse files
committed
prettier
1 parent 4ec3329 commit 8d53f5d

File tree

11 files changed

+80
-66
lines changed

11 files changed

+80
-66
lines changed

ndk-cache-dexie/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# @nostr-dev-kit/ndk-cache-dexie
22

3+
## 2.5.16-rc1.0
4+
5+
### Patch Changes
6+
7+
- 9d1a79c: performance improvements
8+
39
## 2.5.15
410

511
### Patch Changes

ndk-cache-dexie/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nostr-dev-kit/ndk-cache-dexie",
3-
"version": "2.5.15",
3+
"version": "2.5.16-rc1.0",
44
"description": "NDK Dexie Cache Adapter",
55
"license": "MIT",
66
"docs": "typedoc",

ndk-cache-dexie/src/index.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ describe("byKinds performance", () => {
8484
const startTime = Math.floor(Date.now() / 1000);
8585
const eventCount = 5000;
8686
const targetKind = 1;
87-
87+
8888
// Measure time before adding events
8989
const addStart = performance.now();
90-
90+
9191
// Add a large number of events with the same kind
9292
for (let i = 0; i < eventCount; i++) {
9393
const event = new NDKEvent(ndk);
@@ -97,25 +97,25 @@ describe("byKinds performance", () => {
9797
await event.sign();
9898
ndk.cacheAdapter!.setEvent(event, []);
9999
}
100-
100+
101101
const addDuration = performance.now() - addStart;
102102
console.log(`Added ${eventCount} events in ${addDuration}ms`);
103-
103+
104104
// Create a subscription that queries by kind
105105
const subscription = new NDKSubscription(ndk, [{ kinds: [targetKind] }]);
106106
const receiveEventSpy = jest.spyOn(subscription, "eventReceived");
107-
107+
108108
// Measure query time
109109
const queryStart = performance.now();
110110
await ndk.cacheAdapter!.query(subscription);
111111
const queryDuration = performance.now() - queryStart;
112-
112+
113113
console.log(`Query took ${queryDuration}ms`);
114-
114+
115115
// The test passes if the query completes in a reasonable time
116116
// Currently it's failing with 15+ seconds, we want it under 1000ms
117117
expect(queryDuration).toBeLessThan(1000);
118-
118+
119119
// Also verify that we're not getting too many events
120120
// (this would mean our limit filtering is working)
121121
expect(receiveEventSpy).toHaveBeenCalled();

ndk-cache-dexie/src/index.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -527,26 +527,32 @@ export default class NDKCacheAdapterDexie implements NDKCacheAdapter {
527527
return true;
528528
}
529529

530-
private byKinds(filterKeys: Set<string>, filter: NDKFilter, subscription: NDKSubscription): boolean {
530+
private byKinds(
531+
filterKeys: Set<string>,
532+
filter: NDKFilter,
533+
subscription: NDKSubscription
534+
): boolean {
531535
if (!filter.kinds || filterKeys.size !== 1 || !filterKeys.has("kinds")) return false;
532-
536+
533537
const limit = filter.limit || 500;
534538
let totalEvents = 0;
535539
const processedEventIds = new Set<string>();
536-
537-
const sortedKinds = [...filter.kinds].sort((a, b) =>
538-
(this.events.indexes.get("kind")?.get(a)?.size || 0) - (this.events.indexes.get("kind")?.get(b)?.size || 0)
540+
541+
const sortedKinds = [...filter.kinds].sort(
542+
(a, b) =>
543+
(this.events.indexes.get("kind")?.get(a)?.size || 0) -
544+
(this.events.indexes.get("kind")?.get(b)?.size || 0)
539545
);
540-
546+
541547
for (const kind of sortedKinds) {
542548
const events = this.events.getFromIndex("kind", kind);
543549
for (const event of events) {
544550
if (processedEventIds.has(event.id)) continue;
545-
551+
546552
processedEventIds.add(event.id);
547553
foundEvent(subscription, event, event.relay, filter);
548554
totalEvents++;
549-
555+
550556
if (totalEvents >= limit) break;
551557
}
552558
if (totalEvents >= limit) break;

ndk-cache-dexie/src/perf-stats.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class PerfStats {
3232
this.stats.forEach((value, key) => {
3333
stats[key] = {
3434
avg: value.totalTime / value.count,
35-
count: value.count
35+
count: value.count,
3636
};
3737
});
3838
debug("Performance stats:", stats);
@@ -43,4 +43,4 @@ export class PerfStats {
4343
static startOperation() {
4444
return Date.now();
4545
}
46-
}
46+
}

ndk-cache-dexie/src/performance-test.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ async function runTest() {
1313
const startTime = Math.floor(Date.now() / 1000);
1414
const eventCount = 5000;
1515
const targetKind = 1;
16-
16+
1717
console.log(`Adding ${eventCount} events to the cache...`);
1818
const addStart = performance.now();
19-
19+
2020
for (let i = 0; i < eventCount; i++) {
2121
const event = new NDKEvent(ndk);
2222
event.kind = targetKind;
@@ -25,48 +25,47 @@ async function runTest() {
2525
await event.sign();
2626
ndk.cacheAdapter!.setEvent(event, []);
2727
}
28-
28+
2929
const addDuration = performance.now() - addStart;
3030
console.log(`Added ${eventCount} events in ${addDuration.toFixed(2)}ms`);
31-
31+
3232
// Create a subscription that queries by kind
3333
const subscription = new NDKSubscription(ndk, [{ kinds: [targetKind] }]);
34-
34+
3535
// Track received events
3636
let receivedEvents = 0;
3737
const originalEventReceived = subscription.eventReceived;
38-
subscription.eventReceived = function(...args) {
38+
subscription.eventReceived = function (...args) {
3939
receivedEvents++;
4040
return originalEventReceived.apply(this, args);
4141
};
42-
42+
4343
// Measure query time
44-
console.log('Executing query by kinds...');
44+
console.log("Executing query by kinds...");
4545
const queryStart = performance.now();
4646
await ndk.cacheAdapter!.query(subscription);
4747
const queryDuration = performance.now() - queryStart;
48-
48+
4949
console.log(`Query took ${queryDuration.toFixed(2)}ms, received ${receivedEvents} events`);
50-
50+
5151
if (queryDuration < 1000) {
52-
console.log('✅ PASS: Query completed in less than 1000ms');
52+
console.log("✅ PASS: Query completed in less than 1000ms");
5353
} else {
54-
console.log('❌ FAIL: Query took too long (> 1000ms)');
54+
console.log("❌ FAIL: Query took too long (> 1000ms)");
5555
}
56-
56+
5757
if (receivedEvents > 0 && receivedEvents <= 500) {
58-
console.log('✅ PASS: Received an appropriate number of events (≤ 500)');
58+
console.log("✅ PASS: Received an appropriate number of events (≤ 500)");
5959
} else {
60-
console.log('❌ FAIL: Received too many events or none at all');
60+
console.log("❌ FAIL: Received too many events or none at all");
6161
}
62-
62+
6363
// Clean up the database to avoid affecting other tests
6464
await db.delete();
65-
6665
} catch (error) {
67-
console.error('Test failed with error:', error);
66+
console.error("Test failed with error:", error);
6867
}
6968
}
7069

7170
// Run the test
72-
runTest().catch(console.error);
71+
runTest().catch(console.error);
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, it, expect, beforeAll } from 'vitest';
1+
import { describe, it, expect, beforeAll } from "vitest";
22
import NDK, { NDKEvent, NDKPrivateKeySigner, NDKSubscription } from "@nostr-dev-kit/ndk";
33
import NDKCacheAdapterDexie from "../src/index";
44

@@ -7,17 +7,17 @@ const ndk = new NDK();
77
ndk.signer = NDKPrivateKeySigner.generate();
88
ndk.cacheAdapter = new NDKCacheAdapterDexie();
99

10-
describe('Cache performance tests', () => {
11-
it('should handle large number of events without freezing with byKinds', async () => {
10+
describe("Cache performance tests", () => {
11+
it("should handle large number of events without freezing with byKinds", async () => {
1212
// Create a large number of events to trigger the performance issue
1313
const startTime = Math.floor(Date.now() / 1000);
1414
const eventCount = 5000;
1515
const targetKind = 1;
16-
16+
1717
// Add a large number of events with the same kind
1818
console.log(`Adding ${eventCount} events to the cache...`);
1919
const addStart = performance.now();
20-
20+
2121
for (let i = 0; i < eventCount; i++) {
2222
const event = new NDKEvent(ndk);
2323
event.kind = targetKind;
@@ -26,36 +26,36 @@ describe('Cache performance tests', () => {
2626
await event.sign();
2727
ndk.cacheAdapter!.setEvent(event, []);
2828
}
29-
29+
3030
const addDuration = performance.now() - addStart;
3131
console.log(`Added ${eventCount} events in ${addDuration.toFixed(2)}ms`);
32-
32+
3333
// Create a subscription that queries by kind
3434
const subscription = new NDKSubscription(ndk, [{ kinds: [targetKind] }]);
35-
35+
3636
// Spy on the eventReceived method
3737
let receivedEvents = 0;
3838
const originalEventReceived = subscription.eventReceived;
39-
subscription.eventReceived = function(...args) {
39+
subscription.eventReceived = function (...args) {
4040
receivedEvents++;
4141
return originalEventReceived.apply(this, args);
4242
};
43-
43+
4444
// Measure query time
45-
console.log('Executing query by kinds...');
45+
console.log("Executing query by kinds...");
4646
const queryStart = performance.now();
4747
await ndk.cacheAdapter!.query(subscription);
4848
const queryDuration = performance.now() - queryStart;
49-
49+
5050
console.log(`Query took ${queryDuration.toFixed(2)}ms, received ${receivedEvents} events`);
51-
51+
5252
// The test passes if the query completes in a reasonable time
5353
// Currently it's failing with 15+ seconds, we want it under 1000ms
5454
expect(queryDuration).toBeLessThan(1000);
55-
55+
5656
// Also verify that we're not getting too many events
5757
// (this would mean our limit filtering is working)
5858
expect(receivedEvents).toBeGreaterThan(0);
5959
expect(receivedEvents).toBeLessThanOrEqual(500); // Default limit
6060
});
61-
});
61+
});

ndk-cache-dexie/test/setup.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import 'fake-indexeddb/auto';
2-
import { vi } from 'vitest';
1+
import "fake-indexeddb/auto";
2+
import { vi } from "vitest";
33

44
// Mock the debug module
5-
vi.mock('debug', () => {
5+
vi.mock("debug", () => {
66
return {
77
default: () => {
88
const debugFn = (...args: any[]) => {
9-
console.log('[debug]', ...args);
9+
console.log("[debug]", ...args);
1010
};
1111
debugFn.extend = () => debugFn;
1212
return debugFn;
13-
}
13+
},
1414
};
15-
});
15+
});

ndk-cache-dexie/vitest.config.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { defineConfig } from 'vitest/config';
1+
import { defineConfig } from "vitest/config";
22

33
export default defineConfig({
44
test: {
5-
environment: 'node',
6-
setupFiles: ['./test/setup.ts'],
5+
environment: "node",
6+
setupFiles: ["./test/setup.ts"],
77
globals: true,
88
},
9-
});
9+
});

ndk-mobile/src/cache-adapter/search-profiles.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import { NDKCacheAdapterSqlite, NDKSqliteProfileRecord, NDKUserProfile } from ".
33
/**
44
* Convenience method to search for profiles in the database.
55
*/
6-
export function searchProfiles(adapter: NDKCacheAdapterSqlite, query: string): [string, NDKUserProfile][] {
6+
export function searchProfiles(
7+
adapter: NDKCacheAdapterSqlite,
8+
query: string
9+
): [string, NDKUserProfile][] {
710
const pubkeys = adapter.db.getAllSync(
811
`SELECT * FROM profiles WHERE name LIKE ? OR about LIKE ? OR nip05 LIKE ? OR display_name LIKE ?`,
912
[`%${query}%`, `%${query}%`, `%${query}%`, `%${query}%`]

ndk-mobile/src/signers/nip55.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export class NDKNip55Signer implements NDKSigner {
88
private _user?: NDKUser;
99
public packageName: string;
1010
private ndk?: NDK;
11-
11+
1212
constructor(packageName: string, ndk?: NDK) {
1313
this.packageName = packageName;
1414
this.ndk = ndk;
@@ -47,7 +47,7 @@ export class NDKNip55Signer implements NDKSigner {
4747
if (!this._pubkey) throw new Error("Pubkey not ready");
4848
return this._pubkey;
4949
}
50-
50+
5151
/**
5252
* Signs the given Nostr event.
5353
* @param event - The Nostr event to be signed.

0 commit comments

Comments
 (0)