Skip to content

Commit

Permalink
fix: prevent hash of string or number
Browse files Browse the repository at this point in the history
  • Loading branch information
johnhaup committed Dec 21, 2024
1 parent d375ebb commit 00173b6
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 31 deletions.
45 changes: 20 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,37 +57,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.071 | 0.070 |
| Get | 0.000 | 0.069 | 0.069 |
| Delete | 0.000 | 0.070 | 0.069 |
| 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.070 | 0.070 |

#### Results for 1,000 entries

| 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 |
| Operation | Map (ms) | DopeMap (ms) | Difference (ms) |
|-----------|-----------------|--------------|-----------------|
| Set | 0.010 | 0.733 | 0.723 |
| Get | 0.000 | 0.705 | 0.705 |
| Delete | 0.005 | 0.718 | 0.713 |

#### Results for 10,000 entries

| 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 |
| Operation | Map (ms) | DopeMap (ms) | Difference (ms) |
|-----------|-----------------|--------------|-----------------|
| Set | 0.163 | 7.741 | 7.578 |
| Get | 0.008 | 7.324 | 7.316 |
| Delete | 0.053 | 7.663 | 7.610 |

#### Results for 100,000 entries

| 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 |
| Operation | Map (ms) | DopeMap (ms) | Difference (ms) |
|-----------|-----------------|--------------|-----------------|
| Set | 1.852 | 96.536 | 94.684 |
| Get | 0.406 | 92.808 | 92.402 |
| Delete | 0.552 | 78.975 | 78.423 |

<!-- BENCHMARK RESULTS END -->
13 changes: 7 additions & 6 deletions __tests__/arrayArgs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
describe("Array arguments", () => {
type TestValue = { [key: number]: string };
let dopeMap: DopeMap<TestValue>;
const validKeys = new Set(["string", "number"]);

beforeAll(() => {
dopeMap = new DopeMap<TestValue>();
Expand All @@ -26,7 +27,7 @@ describe("Array arguments", () => {
expect(firstItem).toHaveProperty("done");
expect(Array.isArray(firstItem.value)).toBe(true);
expect(firstItem.value.length).toBe(2);
expect(typeof firstItem.value[0]).toBe("string");
expect(validKeys.has(typeof firstItem.value[0])).toBe(true);
expect(firstItem.value[1]).toBe(weezerValue);

let count = 0;
Expand All @@ -44,9 +45,9 @@ describe("Array arguments", () => {

expect(Array.isArray(entries)).toBe(true);
expect(entries.length).toBe(2);
expect(typeof entries[0][0]).toBe("string");
expect(validKeys.has(typeof entries[0][0])).toBe(true);
expect(entries[0][1]).toEqual(weezerValue);
expect(typeof entries[1][0]).toBe("string");
expect(validKeys.has(typeof entries[1][0])).toBe(true);
expect(entries[1][1]).toEqual(nirvanaValue);
});

Expand All @@ -58,11 +59,11 @@ describe("Array arguments", () => {
const firstItem = keys.next();
expect(firstItem).toHaveProperty("value");
expect(firstItem).toHaveProperty("done");
expect(typeof firstItem.value).toBe("string");
expect(validKeys.has(typeof firstItem.value)).toBe(true);

let count = 0;
for (const entry of keys) {
expect(typeof entry).toBe("string");
expect(validKeys.has(typeof entry)).toBe(true);
count++;
}

Expand All @@ -75,7 +76,7 @@ describe("Array arguments", () => {
expect(Array.isArray(keys)).toBe(true);
expect(keys.length).toBe(2);
keys.forEach((key) => {
expect(typeof key).toBe("string");
expect(validKeys.has(typeof key)).toBe(true);
expect(dopeMap.has(key)).toBe(true);
});
});
Expand Down
Binary file modified dope-badges.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface DopeMapConfig {
export default class DopeMap<V> {
private dopeMap: Map<HashedKey, V>;
private hashFunction: HashFunction = hashIt;
private primitiveKeys = new Set(["string", "number"]);

constructor(config: DopeMapConfig = {}) {
this.validateHashFunction(config.hashFunction);
Expand All @@ -33,6 +34,10 @@ export default class DopeMap<V> {
}

private getHashedKey(key: DopeKey) {
if (this.primitiveKeys.has(typeof key)) {
return key as HashedKey;
}

return this.hashFunction(key);
}

Expand Down

0 comments on commit 00173b6

Please sign in to comment.