Skip to content

Commit

Permalink
fix: remove string prefix and transformation in getKey
Browse files Browse the repository at this point in the history
  • Loading branch information
johnhaup committed Dec 21, 2024
1 parent 0ac6423 commit 58c9329
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 41 deletions.
45 changes: 20 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,37 +39,32 @@ const dopeMap = new DopeMap({ hashFunction: blazeHasher });
## Benchmarks

<!-- BENCHMARK RESULTS START -->

#### Results for 100 entries

| Operation | Map (ms) | DopeMap (ms) | Difference (ms) |
| --------- | -------- | ------------ | --------------- |
| Set | 0.001 | 0.083 | 0.083 |
| Get | 0.000 | 0.070 | 0.070 |
| Delete | 0.000 | 0.076 | 0.076 |
| Operation | Map (ms) | DopeMap (ms) | Difference (ms) |
|-----------|-----------------|--------------|-----------------|
| Set | 0.001 | 0.071 | 0.070 |
| Get | 0.000 | 0.069 | 0.069 |
| Delete | 0.000 | 0.070 | 0.069 |

#### Results for 1,000 entries

| Operation | Map (ms) | DopeMap (ms) | Difference (ms) |
| --------- | -------- | ------------ | --------------- |
| Set | 0.009 | 0.862 | 0.853 |
| Get | 0.000 | 0.737 | 0.736 |
| Delete | 0.005 | 0.784 | 0.779 |
| Operation | Map (ms) | DopeMap (ms) | Difference (ms) |
|-----------|-----------------|--------------|-----------------|
| Set | 0.009 | 0.729 | 0.720 |
| Get | 0.000 | 0.699 | 0.699 |
| Delete | 0.005 | 0.704 | 0.699 |

#### Results for 10,000 entries

| Operation | Map (ms) | DopeMap (ms) | Difference (ms) |
| --------- | -------- | ------------ | --------------- |
| Set | 0.163 | 9.754 | 9.591 |
| Get | 0.007 | 9.503 | 9.495 |
| Delete | 0.052 | 8.681 | 8.629 |
| Operation | Map (ms) | DopeMap (ms) | Difference (ms) |
|-----------|-----------------|--------------|-----------------|
| Set | 0.164 | 7.514 | 7.350 |
| Get | 0.007 | 7.218 | 7.210 |
| Delete | 0.053 | 7.314 | 7.261 |

#### Results for 100,000 entries

| Operation | Map (ms) | DopeMap (ms) | Difference (ms) |
| --------- | -------- | ------------ | --------------- |
| Set | 1.728 | 112.848 | 111.120 |
| Get | 0.361 | 112.191 | 111.830 |
| Delete | 0.602 | 95.297 | 94.695 |
| Operation | Map (ms) | DopeMap (ms) | Difference (ms) |
|-----------|-----------------|--------------|-----------------|
| Set | 1.762 | 94.037 | 92.274 |
| Get | 0.369 | 88.134 | 87.765 |
| Delete | 0.656 | 80.199 | 79.543 |

<!-- BENCHMARK RESULTS END -->
30 changes: 14 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import hashIt from "hash-it";

type DopeKey = unknown;
type HashFunction = (args: unknown) => string | number;
type HashedKey = string | number;
type HashFunction = (args: unknown) => HashedKey;

interface DopeMapConfig {
/**
Expand All @@ -11,8 +12,7 @@ interface DopeMapConfig {
}

export default class DopeMap<V> {
private hashPrefix = "_dope:";
private dopeMap: Map<string, V>;
private dopeMap: Map<HashedKey, V>;
private hashFunction: HashFunction = hashIt;

constructor(config: DopeMapConfig = {}) {
Expand All @@ -32,12 +32,8 @@ export default class DopeMap<V> {
}
}

private getHashedKey(key: DopeKey): string {
if (typeof key === "string" && key.startsWith(this.hashPrefix)) {
return key;
}

return `${this.hashPrefix}${this.hashFunction(key)}`;
private getHashedKey(key: DopeKey) {
return this.hashFunction(key);
}

set(key: DopeKey, value: V): void {
Expand Down Expand Up @@ -79,22 +75,24 @@ export default class DopeMap<V> {
return this.dopeMap.clear();
}

entries(asArray: true): [string, V][];
entries(asArray?: false): IterableIterator<[string, V]>;
entries(asArray?: boolean): [string, V][] | IterableIterator<[string, V]> {
entries(asArray: true): [HashedKey, V][];
entries(asArray?: false): IterableIterator<[HashedKey, V]>;
entries(
asArray?: boolean
): [HashedKey, V][] | IterableIterator<[HashedKey, V]> {
if (asArray) {
return Array.from(this.dopeMap.entries());
}
return this.dopeMap.entries();
}

forEach(...args: Parameters<Map<string, V>["forEach"]>) {
forEach(...args: Parameters<Map<string | number, V>["forEach"]>) {
return this.dopeMap.forEach(...args);
}

keys(asArray: true): string[];
keys(asArray?: false): IterableIterator<string>;
keys(asArray?: boolean): string[] | IterableIterator<string> {
keys(asArray: true): HashedKey[];
keys(asArray?: false): IterableIterator<HashedKey>;
keys(asArray?: boolean): HashedKey[] | IterableIterator<HashedKey> {
if (asArray) {
return Array.from(this.dopeMap.keys());
}
Expand Down

0 comments on commit 58c9329

Please sign in to comment.