From 91206ebf93f3415f2dcde735faf5d4eeb16d87ea Mon Sep 17 00:00:00 2001 From: daishi Date: Sat, 12 Oct 2024 15:47:25 +0900 Subject: [PATCH] benchmark minSize --- benchmark/proxyMap.benchmark.ts | 82 ++++++++++++++------------ src/vanilla/utils/proxyMap-indexMap.ts | 13 ++-- 2 files changed, 52 insertions(+), 43 deletions(-) diff --git a/benchmark/proxyMap.benchmark.ts b/benchmark/proxyMap.benchmark.ts index 7e756c91..3c5a77a0 100644 --- a/benchmark/proxyMap.benchmark.ts +++ b/benchmark/proxyMap.benchmark.ts @@ -17,33 +17,39 @@ function generateTestData(size: number): [number, number][] { const suite = new Benchmark.Suite() // Test parameters -const TEST_SIZES = [1000, 10000, 30000, 50000, 100000] +const TEST_SIZES = [1000, 10000, 30000] TEST_SIZES.forEach((size) => { const testData = generateTestData(size) - // // Benchmark for insertion - // suite.add(`Insertion - Native Map (${size} items)`, () => { - // const map = new Map() - // testData.forEach(([key, value]) => { - // map.set(key, value) - // }) - // }) - - suite.add(`Insertion - New proxyMap (${size} items)`, () => { - const map = newProxyMap() + // Benchmark for insertion + suite.add(`Insertion - Native Map (${size} items)`, () => { + const map = new Map() testData.forEach(([key, value]) => { map.set(key, value) }) }) - suite.add(`Insertion - Chunked proxyMap (${size} items)`, () => { - const map = chunkedProxyMap() - testData.forEach(([key, value]) => { - map.set(key, value) - }) + const MIN_SIZES = [0, 1000, 10000, 30000] + MIN_SIZES.forEach((minSize) => { + suite.add( + `Insertion - New proxyMap (${size} items) (minSize=${minSize})`, + () => { + const map = newProxyMap(null, { minSize }) + testData.forEach(([key, value]) => { + map.set(key, value) + }) + }, + ) }) + // suite.add(`Insertion - Chunked proxyMap (${size} items)`, () => { + // const map = chunkedProxyMap() + // testData.forEach(([key, value]) => { + // map.set(key, value) + // }) + // }) + // suite.add(`Insertion - Btree proxyMap (${size} items)`, () => { // const map = btreeProxyMap() // testData.forEach(([key, value]) => { @@ -127,17 +133,17 @@ TEST_SIZES.forEach((size) => { // }, // ) - suite.add( - `Insertion, Retrieval, and Deletion - New ProxyMap (${size} items)`, - () => { - const map = newProxyMap(deepClone(testData)) - testData.forEach(([key, value]) => { - map.set(key, value) - map.get(key) - map.delete(key) - }) - }, - ) + // suite.add( + // `Insertion, Retrieval, and Deletion - New ProxyMap (${size} items)`, + // () => { + // const map = newProxyMap(deepClone(testData)) + // testData.forEach(([key, value]) => { + // map.set(key, value) + // map.get(key) + // map.delete(key) + // }) + // }, + // ) // suite.add('Insertion, Retrieval, and Deletion - Btree ProxyMap', () => { // const map = btreeProxyMap(deepClone(testData)) @@ -148,17 +154,17 @@ TEST_SIZES.forEach((size) => { // }) // }) - suite.add( - `Insertion, Retrieval, and Deletion - Chunked ProxyMap (${size} items)`, - () => { - const map = chunkedProxyMap(deepClone(testData)) - testData.forEach(([key, value]) => { - map.set(key, value) - map.get(key) - map.delete(key) - }) - }, - ) + // suite.add( + // `Insertion, Retrieval, and Deletion - Chunked ProxyMap (${size} items)`, + // () => { + // const map = chunkedProxyMap(deepClone(testData)) + // testData.forEach(([key, value]) => { + // map.set(key, value) + // map.get(key) + // map.delete(key) + // }) + // }, + // ) }) // Run the benchmarks diff --git a/src/vanilla/utils/proxyMap-indexMap.ts b/src/vanilla/utils/proxyMap-indexMap.ts index b1c51fc9..de1ee204 100644 --- a/src/vanilla/utils/proxyMap-indexMap.ts +++ b/src/vanilla/utils/proxyMap-indexMap.ts @@ -9,12 +9,15 @@ type InternalProxyObject = Map & { toJSON: () => Map } -const MIN_DATA_SIZE = 1000 - -export function proxyMap(entries?: Iterable<[K, V]> | undefined | null) { - const initialData: Array<[K, V] | undefined> = new Array(MIN_DATA_SIZE).fill( +export function proxyMap( + entries?: Iterable<[K, V]> | undefined | null, + options?: { minSize: number }, +) { + const minDataSize = options?.minSize ?? 1000 + const initialData: Array<[K, V] | undefined> = new Array(minDataSize).fill( undefined, ) + let initialNextIndex = 0 const indexMap = new Map() const emptyIndexes: number[] = [] @@ -95,7 +98,7 @@ export function proxyMap(entries?: Iterable<[K, V]> | undefined | null) { throw new Error('Cannot perform mutations on a snapshot') } indexMap.clear() - this.data.splice(MIN_DATA_SIZE).fill(undefined) + this.data.splice(minDataSize).fill(undefined) this.nextIndex = 0 emptyIndexes.splice(0) },