Skip to content

Commit

Permalink
benchmark minSize
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Oct 12, 2024
1 parent bd6cbf3 commit 91206eb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 43 deletions.
82 changes: 44 additions & 38 deletions benchmark/proxyMap.benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number, number>()
// testData.forEach(([key, value]) => {
// map.set(key, value)
// })
// })

suite.add(`Insertion - New proxyMap (${size} items)`, () => {
const map = newProxyMap<number, number>()
// Benchmark for insertion
suite.add(`Insertion - Native Map (${size} items)`, () => {
const map = new Map<number, number>()
testData.forEach(([key, value]) => {
map.set(key, value)
})
})

suite.add(`Insertion - Chunked proxyMap (${size} items)`, () => {
const map = chunkedProxyMap<number, number>()
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<number, number>(null, { minSize })
testData.forEach(([key, value]) => {
map.set(key, value)
})
},
)
})

// suite.add(`Insertion - Chunked proxyMap (${size} items)`, () => {
// const map = chunkedProxyMap<number, number>()
// testData.forEach(([key, value]) => {
// map.set(key, value)
// })
// })

// suite.add(`Insertion - Btree proxyMap (${size} items)`, () => {
// const map = btreeProxyMap<number, number>()
// testData.forEach(([key, value]) => {
Expand Down Expand Up @@ -127,17 +133,17 @@ TEST_SIZES.forEach((size) => {
// },
// )

suite.add(
`Insertion, Retrieval, and Deletion - New ProxyMap (${size} items)`,
() => {
const map = newProxyMap<number, number>(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<number, number>(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<number, number>(deepClone(testData))
Expand All @@ -148,17 +154,17 @@ TEST_SIZES.forEach((size) => {
// })
// })

suite.add(
`Insertion, Retrieval, and Deletion - Chunked ProxyMap (${size} items)`,
() => {
const map = chunkedProxyMap<number, number>(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<number, number>(deepClone(testData))
// testData.forEach(([key, value]) => {
// map.set(key, value)
// map.get(key)
// map.delete(key)
// })
// },
// )
})

// Run the benchmarks
Expand Down
13 changes: 8 additions & 5 deletions src/vanilla/utils/proxyMap-indexMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ type InternalProxyObject<K, V> = Map<K, V> & {
toJSON: () => Map<K, V>
}

const MIN_DATA_SIZE = 1000

export function proxyMap<K, V>(entries?: Iterable<[K, V]> | undefined | null) {
const initialData: Array<[K, V] | undefined> = new Array(MIN_DATA_SIZE).fill(
export function proxyMap<K, V>(
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<K, number>()
const emptyIndexes: number[] = []
Expand Down Expand Up @@ -95,7 +98,7 @@ export function proxyMap<K, V>(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)
},
Expand Down

0 comments on commit 91206eb

Please sign in to comment.