Skip to content

Commit 98914fd

Browse files
committed
Runtime: 211 ms (Top 9.0%) | Memory: 51.68 MB (Top 72.7%)
1 parent 640dc53 commit 98914fd

File tree

1 file changed

+11
-51
lines changed

1 file changed

+11
-51
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,19 @@
1-
// Runtime: 360 ms (Top 51.28%) | Memory: 62 MB (Top 5.13%)
2-
/**
3-
* @param {number} timeToLive
4-
*/
1+
// Runtime: 211 ms (Top 9.0%) | Memory: 51.68 MB (Top 72.7%)
2+
3+
// O(n)
54
var AuthenticationManager = function(timeToLive) {
6-
this.timeToLive=timeToLive;
7-
this.tokens=new Map();
5+
this.ttl = timeToLive;
6+
this.map = {};
87
};
9-
10-
/**
11-
* @param {string} tokenId
12-
* @param {number} currentTime
13-
* @return {void}
14-
*/
158
AuthenticationManager.prototype.generate = function(tokenId, currentTime) {
16-
if(!this.tokens.has(tokenId)){
17-
let tokenInfo={
18-
tokenId:tokenId,
19-
expiresAt:currentTime+this.timeToLive
20-
}
21-
this.tokens.set(tokenId,tokenInfo);
22-
}
9+
this.map[tokenId] = currentTime + this.ttl;
2310
};
24-
25-
/**
26-
* @param {string} tokenId
27-
* @param {number} currentTime
28-
* @return {void}
29-
*/
3011
AuthenticationManager.prototype.renew = function(tokenId, currentTime) {
31-
if(!this.tokens.has(tokenId)) return;
32-
33-
let token=this.tokens.get(tokenId);
34-
if(token.expiresAt>currentTime){
35-
this.tokens.set(tokenId,{...token,expiresAt:currentTime+this.timeToLive})
36-
}else{
37-
this.tokens.delete(tokenId);
12+
let curr = this.map[tokenId];
13+
if (curr > currentTime) {
14+
this.generate(tokenId, currentTime);
3815
}
3916
};
40-
41-
/**
42-
* @param {number} currentTime
43-
* @return {number}
44-
*/
4517
AuthenticationManager.prototype.countUnexpiredTokens = function(currentTime) {
46-
let activeTokensCount=0;
47-
for(let [key,token] of this.tokens.entries()){
48-
if(token.expiresAt>currentTime) activeTokensCount++;
49-
}
50-
return activeTokensCount;
51-
};
52-
53-
/**
54-
* Your AuthenticationManager object will be instantiated and called as such:
55-
* var obj = new AuthenticationManager(timeToLive)
56-
* obj.generate(tokenId,currentTime)
57-
* obj.renew(tokenId,currentTime)
58-
* var param_3 = obj.countUnexpiredTokens(currentTime)
59-
*/
18+
return Object.keys(this.map).filter(key => this.map[key] > currentTime).length;
19+
};

0 commit comments

Comments
 (0)