Skip to content

Commit e9eeab2

Browse files
authored
perf: hash table delete key just query once (#23)
* perf: hash table delete key query once * docs: update comment
1 parent c41246f commit e9eeab2

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

src/hash-table/index.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,19 @@ export class HashTable {
6969
return node ? node.value.value : undefined
7070
}
7171

72+
/**
73+
* 删除指定的值
74+
* @param key 需要删除的值
75+
*/
7276
public delete(key: string): any | null {
7377
const keyHash = this.hash(key)
7478
delete this.keys[key]
7579
const bucketLinkedList = this.buckets[keyHash]
76-
const node = bucketLinkedList.find(key, (nodeValue: any) => {
80+
const deleteNode = bucketLinkedList.delete(key, (nodeValue: any) => {
7781
return nodeValue.key === key
7882
})
7983

80-
if (node) {
81-
const deleteNode = bucketLinkedList.delete(key, (nodeValue: any) => {
82-
return nodeValue.key === key
83-
})
84-
console.log(deleteNode)
85-
return deleteNode ? deleteNode.value.value : null
86-
} else {
87-
return null
88-
}
84+
return deleteNode ? deleteNode.value.value : null
8985
}
9086

9187
/**

0 commit comments

Comments
 (0)