Skip to content

Commit

Permalink
chore: add primitive benchmark tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johnhaup committed Dec 24, 2024
1 parent 3a9ee36 commit 48eacbc
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 103 deletions.
87 changes: 55 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,37 +88,60 @@ The following table summarizes the **methods** and **properties** of `DopeMap`,
_Each Dope/Map grows to the entry size. Averages of method time are below._

<!-- BENCHMARK RESULTS START -->

#### Results for 100 entries

| Operation | Map (ms) | DopeMap (ms) | Difference (ms) |
| --------- | -------- | ------------ | --------------- |
| Set | 0.001 | 0.074 | 0.073 |
| Get | 0.000 | 0.074 | 0.074 |
| Delete | 0.000 | 0.072 | 0.071 |

#### Results for 1,000 entries

| Operation | Map (ms) | DopeMap (ms) | Difference (ms) |
| --------- | -------- | ------------ | --------------- |
| Set | 0.010 | 0.743 | 0.734 |
| Get | 0.000 | 0.789 | 0.789 |
| Delete | 0.005 | 0.718 | 0.713 |

#### Results for 10,000 entries

| Operation | Map (ms) | DopeMap (ms) | Difference (ms) |
| --------- | -------- | ------------ | --------------- |
| Set | 0.165 | 7.916 | 7.751 |
| Get | 0.007 | 7.692 | 7.684 |
| Delete | 0.054 | 7.313 | 7.259 |

#### Results for 100,000 entries

| Operation | Map (ms) | DopeMap (ms) | Difference (ms) |
| --------- | -------- | ------------ | --------------- |
| Set | 1.686 | 96.664 | 94.978 |
| Get | 0.323 | 96.374 | 96.052 |
| Delete | 0.565 | 79.796 | 79.231 |
#### objects keys / 100 entries
| Operation | Map (ms) | DopeMap (ms) | Difference (ms) |
|-----------|-----------------|--------------|-----------------|
| Set | 0.001 | 0.072 | 0.071 |
| Get | 0.000 | 0.072 | 0.072 |
| Delete | 0.000 | 0.071 | 0.071 |

#### primitives keys / 100 entries
| Operation | Map (ms) | DopeMap (ms) | Difference (ms) |
|-----------|-----------------|--------------|-----------------|
| Set | 0.000 | 0.000 | 0.000 |
| Get | 0.000 | 0.000 | 0.000 |
| Delete | 0.000 | 0.000 | -0.000 |

#### objects keys / 1,000 entries
| Operation | Map (ms) | DopeMap (ms) | Difference (ms) |
|-----------|-----------------|--------------|-----------------|
| Set | 0.010 | 0.739 | 0.729 |
| Get | 0.001 | 0.733 | 0.732 |
| Delete | 0.005 | 0.716 | 0.710 |

#### primitives keys / 1,000 entries
| Operation | Map (ms) | DopeMap (ms) | Difference (ms) |
|-----------|-----------------|--------------|-----------------|
| Set | 0.002 | 0.002 | -0.000 |
| Get | 0.001 | 0.002 | 0.002 |
| Delete | 0.002 | 0.002 | -0.000 |

#### objects keys / 10,000 entries
| Operation | Map (ms) | DopeMap (ms) | Difference (ms) |
|-----------|-----------------|--------------|-----------------|
| Set | 0.162 | 7.823 | 7.661 |
| Get | 0.008 | 7.749 | 7.741 |
| Delete | 0.052 | 7.302 | 7.250 |

#### primitives keys / 10,000 entries
| Operation | Map (ms) | DopeMap (ms) | Difference (ms) |
|-----------|-----------------|--------------|-----------------|
| Set | 0.023 | 0.022 | -0.000 |
| Get | 0.005 | 0.023 | 0.017 |
| Delete | 0.022 | 0.022 | -0.000 |

#### objects keys / 100,000 entries
| Operation | Map (ms) | DopeMap (ms) | Difference (ms) |
|-----------|-----------------|--------------|-----------------|
| Set | 1.636 | 90.981 | 89.345 |
| Get | 0.314 | 87.615 | 87.301 |
| Delete | 0.556 | 77.312 | 76.757 |

#### primitives keys / 100,000 entries
| Operation | Map (ms) | DopeMap (ms) | Difference (ms) |
|-----------|-----------------|--------------|-----------------|
| Set | 0.224 | 0.225 | 0.002 |
| Get | 0.058 | 0.226 | 0.169 |
| Delete | 0.225 | 0.223 | -0.002 |

<!-- BENCHMARK RESULTS END -->
161 changes: 90 additions & 71 deletions benchmarks/benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,80 +33,99 @@ function generateMixedKeys(size: number): object[] {
return keys;
}

function generatePrimitiveKeys(size: number): string[] | number[] {
return new Array(size).map((s) => (s % 2 === 0 ? s : `${s}_${s}:wavves`));
}

const KEY_CONFIGS = [
{
title: "objects",
generateKeys: generateMixedKeys,
},
{
title: "primitives",
generateKeys: generatePrimitiveKeys,
},
];

const resultsTable: string[] = [];

SIZES.forEach((size) => {
console.log(`Running benchmarks for size: ${size} entries with mixed keys`);
const suite = new Benchmark.Suite();

const nativeMap = new Map<object, string>();
const customMap = new DopeMap<string>();

const objectKeys = generateMixedKeys(size);
const testValue = "testValue";

suite
.add(`Map - Set (${size} entries)`, function () {
objectKeys.forEach((key) => nativeMap.set(key, testValue));
})
.add(`DopeMap - Set (${size} entries)`, function () {
objectKeys.forEach((key) => customMap.set(key, testValue));
})
.add(`Map - Get (${size} entries)`, function () {
objectKeys.forEach((key) => nativeMap.get(key));
})
.add(`DopeMap - Get (${size} entries)`, function () {
objectKeys.forEach((key) => customMap.get(key));
})
.add(`Map - Delete (${size} entries)`, function () {
objectKeys.forEach((key) => nativeMap.delete(key));
})
.add(`DopeMap - Delete (${size} entries)`, function () {
objectKeys.forEach((key) => customMap.delete(key));
})
.on("complete", function () {
console.log(`Results for ${size} entries:\n`);

const results: { [name: string]: number } = {};

this.forEach((bench: Target) => {
if (bench.hz && bench.name) {
const opsPerSec = bench.hz;
const avgTimeMs = (1 / opsPerSec) * 1000;
results[bench.name] = avgTimeMs;

console.log(
`${bench.name}: ${opsPerSec.toFixed(
2
)} ops/sec (${avgTimeMs.toFixed(4)} ms per operation)`
);
}
});

const nativeSet = results[`Map - Set (${size} entries)`];
const dopeSet = results[`DopeMap - Set (${size} entries)`];
const nativeGet = results[`Map - Get (${size} entries)`];
const dopeGet = results[`DopeMap - Get (${size} entries)`];
const nativeDelete = results[`Map - Delete (${size} entries)`];
const dopeDelete = results[`DopeMap - Delete (${size} entries)`];

resultsTable.push(
`#### Results for ${size.toLocaleString()} entries`,
`| Operation | Map (ms) | DopeMap (ms) | Difference (ms) |`,
`|-----------|-----------------|--------------|-----------------|`,
`| Set | ${nativeSet.toFixed(3)} | ${dopeSet.toFixed(
3
)} | ${(dopeSet - nativeSet).toFixed(3)} |`,
`| Get | ${nativeGet.toFixed(3)} | ${dopeGet.toFixed(
3
)} | ${(dopeGet - nativeGet).toFixed(3)} |`,
`| Delete | ${nativeDelete.toFixed(3)} | ${dopeDelete.toFixed(
3
)} | ${(dopeDelete - nativeDelete).toFixed(3)} |`,
``
);
})
.run({ async: false }); // Ensure benchmarks run sequentially
KEY_CONFIGS.forEach(({ title, generateKeys }) => {
console.log(
`\nRunning benchmarks for size: ${size} entries with ${title} keys`
);
const suite = new Benchmark.Suite();

const nativeMap = new Map<object, string>();
const customMap = new DopeMap<string>();

const generatedKeys = generateKeys(size);
const testValue = "testValue";

suite
.add(`Map - Set (${size} entries)`, function () {
generatedKeys.forEach((key) => nativeMap.set(key, testValue));
})
.add(`DopeMap - Set (${size} entries)`, function () {
generatedKeys.forEach((key) => customMap.set(key, testValue));
})
.add(`Map - Get (${size} entries)`, function () {
generatedKeys.forEach((key) => nativeMap.get(key));
})
.add(`DopeMap - Get (${size} entries)`, function () {
generatedKeys.forEach((key) => customMap.get(key));
})
.add(`Map - Delete (${size} entries)`, function () {
generatedKeys.forEach((key) => nativeMap.delete(key));
})
.add(`DopeMap - Delete (${size} entries)`, function () {
generatedKeys.forEach((key) => customMap.delete(key));
})
.on("complete", function () {
console.log(`Results for ${size} entries:`);

const results: { [name: string]: number } = {};

this.forEach((bench: Target) => {
if (bench.hz && bench.name) {
const opsPerSec = bench.hz;
const avgTimeMs = (1 / opsPerSec) * 1000;
results[bench.name] = avgTimeMs;

console.log(
`${bench.name}: ${opsPerSec.toFixed(
2
)} ops/sec (${avgTimeMs.toFixed(4)} ms per operation)`
);
}
});

const nativeSet = results[`Map - Set (${size} entries)`];
const dopeSet = results[`DopeMap - Set (${size} entries)`];
const nativeGet = results[`Map - Get (${size} entries)`];
const dopeGet = results[`DopeMap - Get (${size} entries)`];
const nativeDelete = results[`Map - Delete (${size} entries)`];
const dopeDelete = results[`DopeMap - Delete (${size} entries)`];

resultsTable.push(
`#### ${title} keys / ${size.toLocaleString()} entries`,
`| Operation | Map (ms) | DopeMap (ms) | Difference (ms) |`,
`|-----------|-----------------|--------------|-----------------|`,
`| Set | ${nativeSet.toFixed(3)} | ${dopeSet.toFixed(
3
)} | ${(dopeSet - nativeSet).toFixed(3)} |`,
`| Get | ${nativeGet.toFixed(3)} | ${dopeGet.toFixed(
3
)} | ${(dopeGet - nativeGet).toFixed(3)} |`,
`| Delete | ${nativeDelete.toFixed(3)} | ${dopeDelete.toFixed(
3
)} | ${(dopeDelete - nativeDelete).toFixed(3)} |`,
``
);
})
.run({ async: false }); // Ensure benchmarks run sequentially
});
});

const __filename = fileURLToPath(import.meta.url);
Expand Down

0 comments on commit 48eacbc

Please sign in to comment.