Skip to content

Commit

Permalink
feat(rr-cache): add various code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
zhibirc committed Dec 6, 2023
1 parent b66c084 commit 5a91b37
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 12 deletions.
19 changes: 19 additions & 0 deletions libs/get-typed-array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const MAX_8BIT_UINT = (1 << 8) - 1;
const MAX_16BIT_UINT = (1 << 16) - 1;
const MAX_32BIT_UINT = (1 << 30) * 4 - 1;

function getTypedArray (n: number) {
switch (true) {
case n - 1 <= MAX_8BIT_UINT:
return new Uint8Array(n);
case n - 1 <= MAX_16BIT_UINT:
return new Uint16Array(n);
case n - 1 <= MAX_32BIT_UINT:
return new Uint32Array(n);
default:
return new Array(n);
}
}


export default getTypedArray;
6 changes: 5 additions & 1 deletion lru-cache/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

## Replacement policy

LRU has a pretty practical replacement policy.
LRU has a pretty practical replacement policy.

## More information

[Mnemonist: LRU Cache](https://yomguithereal.github.io/mnemonist/lru-cache)
4 changes: 2 additions & 2 deletions rr-cache/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TConfigOptions, ICache } from '../libs/types.js';
import { ICache } from '../libs/types.js';
declare class RRCache implements ICache {
#private;
constructor(options: TConfigOptions);
constructor(capacity: number);
get stats(): {
size: number;
capacity: number;
Expand Down
6 changes: 3 additions & 3 deletions rr-cache/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ class RRCache {
#keys;
#freeSlots;
#store;
constructor(options) {
const { capacity } = options;
if (!Number.isInteger(capacity) || capacity <= 0)
constructor(capacity) {
if (!Number.isInteger(capacity) || capacity <= 0) {
throw new Error('invalid "capacity": positive integer expected');
}
this.#hits = 0;
this.#misses = 0;
this.#capacity = capacity;
Expand Down
12 changes: 7 additions & 5 deletions rr-cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ class RRCache implements ICache {
#store;

constructor ( capacity: number ) {
if (!Number.isInteger(capacity) || capacity <= 0) throw new Error('invalid "capacity": positive integer expected');
if (!Number.isInteger(capacity) || capacity <= 0) {
throw new Error('invalid "capacity": positive integer expected');
}

this.#hits = 0;
this.#misses = 0;
Expand All @@ -31,13 +33,13 @@ class RRCache implements ICache {
}

set lock (state: boolean) {
this.#locked = state;
this.#locked = Boolean(state);
}

/**
* Read value stored in cache by assosiated key.
* @param {*} key - cache record's key
* @return {*|null} record's value retrieved by key or null if record is absent
* @return {*|void} record's value retrieved by key or null if record is absent
*/
read (key: any) {
if (this.#store.has(key)) {
Expand All @@ -46,7 +48,6 @@ class RRCache implements ICache {
}

this.#misses += 1;
return null;
}

add (key: any, value: any) {
Expand Down Expand Up @@ -93,10 +94,11 @@ class RRCache implements ICache {
}

/**
* Remove all items from the cache.
* Remove all items from the cache and clear internal structures.
* @return {void}
*/
clear () {
this.#hits = this.#misses = 0;
this.#keys.length = this.#freeSlots.length = 0;
this.#keys.length = this.#capacity;
this.#store.clear();
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
],
"exclude": [
"node_modules",
"libs"
"libs/decorators"
]
}

0 comments on commit 5a91b37

Please sign in to comment.