mirror of
https://github.com/PR0M3TH3AN/bitvid.git
synced 2026-11-04 20:02:26 +00:00
Merge pull request #2126 from PR0M3TH3AN/perf/optimize-nip46-normalization-quadratic-5068119382655595013
⚡ Optimize NIP-46 payload normalization to avoid O(N^2) on large inputs
This commit is contained in:
2 files changed
+29
-1
No files matched your search
@@ -0,0 +1,28 @@
|
||||
|
||||
import { normalizeNip46CiphertextPayload } from "../js/nostr/nip46Client.js";
|
||||
|
||||
const ITERATIONS = 100;
|
||||
const ARRAY_SIZE = 200;
|
||||
|
||||
function runBenchmark() {
|
||||
const largeArray = Array.from({ length: ARRAY_SIZE }, (_, i) => `string_${i}`);
|
||||
|
||||
// Wrap in a structure that triggers the recursive coerce and then the array handling
|
||||
const payload = {
|
||||
data: largeArray
|
||||
};
|
||||
|
||||
const start = performance.now();
|
||||
|
||||
for (let i = 0; i < ITERATIONS; i++) {
|
||||
normalizeNip46CiphertextPayload(payload);
|
||||
}
|
||||
|
||||
const end = performance.now();
|
||||
const duration = end - start;
|
||||
|
||||
console.log(`Time taken for ${ITERATIONS} iterations with array size ${ARRAY_SIZE}: ${duration.toFixed(2)}ms`);
|
||||
console.log(`Average time per iteration: ${(duration / ITERATIONS).toFixed(2)}ms`);
|
||||
}
|
||||
|
||||
runBenchmark();
|
||||
@@ -945,7 +945,7 @@ export function normalizeNip46CiphertextPayload(payload) {
|
||||
}
|
||||
}
|
||||
|
||||
if (scalarEntries.length >= 2) {
|
||||
if (scalarEntries.length >= 2 && scalarEntries.length <= 16) {
|
||||
for (let i = 0; i < scalarEntries.length; i += 1) {
|
||||
for (let j = i + 1; j < scalarEntries.length; j += 1) {
|
||||
addCiphertextWithNonce(scalarEntries[i], scalarEntries[j]);
|
||||
|
||||
Reference in new issue
Block a user