Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/data-structures/hash-table/HashTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class HashTable {
this.buckets = Array(hashTableSize).fill(null).map(() => new LinkedList());

// Just to keep track of all actual keys in a fast way.
this.keys = {};
this.keys = Object.create(null);
}

/**
Expand Down
26 changes: 26 additions & 0 deletions src/data-structures/hash-table/__test__/HashTable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,32 @@ describe('HashTable', () => {
expect(hashTable.has('x')).toBe(false);
});

it('should track prototype-named keys through updates and deletion', () => {
const hashTable = new HashTable(1);
const keys = ['__proto__', 'constructor', 'hasOwnProperty', 'toString'];

keys.forEach((key) => {
expect(hashTable.has(key)).toBe(false);
hashTable.set(key, 'original');
expect(hashTable.has(key)).toBe(true);
expect(hashTable.get(key)).toBe('original');
hashTable.set(key, 'updated');
expect(hashTable.get(key)).toBe('updated');
});

expect(hashTable.getKeys()).toEqual(keys);
expect(hashTable.getValues()).toEqual(keys.map(() => 'updated'));

keys.forEach((key) => {
hashTable.delete(key);
expect(hashTable.has(key)).toBe(false);
expect(hashTable.get(key)).toBeUndefined();
});

expect(hashTable.getKeys()).toEqual([]);
expect(hashTable.getValues()).toEqual([]);
});

it('should get all the values', () => {
const hashTable = new HashTable(3);

Expand Down