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)
5
4
var AuthenticationManager = function ( timeToLive ) {
6
- this . timeToLive = timeToLive ;
7
- this . tokens = new Map ( ) ;
5
+ this . ttl = timeToLive ;
6
+ this . map = { } ;
8
7
} ;
9
-
10
- /**
11
- * @param {string } tokenId
12
- * @param {number } currentTime
13
- * @return {void }
14
- */
15
8
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 ;
23
10
} ;
24
-
25
- /**
26
- * @param {string } tokenId
27
- * @param {number } currentTime
28
- * @return {void }
29
- */
30
11
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 ) ;
38
15
}
39
16
} ;
40
-
41
- /**
42
- * @param {number } currentTime
43
- * @return {number }
44
- */
45
17
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