|
910 | 910 | "tags": ["javascript", "function", "compose", "utility"], |
911 | 911 | "author": "axorax" |
912 | 912 | }, |
913 | | - { |
914 | | - "title": "Rate Limit Function", |
915 | | - "description": "Limits how often a function can be executed within a given time window.", |
916 | | - "code": [ |
917 | | - "const rateLimit = (func, limit, timeWindow) => {", |
918 | | - " let queue = [];", |
919 | | - " setInterval(() => {", |
920 | | - " if (queue.length) {", |
921 | | - " const next = queue.shift();", |
922 | | - " func(...next.args);", |
923 | | - " }", |
924 | | - " }, timeWindow);", |
925 | | - " return (...args) => {", |
926 | | - " if (queue.length < limit) {", |
927 | | - " queue.push({ args });", |
928 | | - " }", |
929 | | - " };", |
930 | | - "};", |
931 | | - "", |
932 | | - "// Usage:", |
933 | | - "const fetchData = () => console.log('Fetching data...');", |
934 | | - "const rateLimitedFetch = rateLimit(fetchData, 2, 1000);", |
935 | | - "setInterval(() => rateLimitedFetch(), 200); // Only calls fetchData twice every second" |
936 | | - ], |
937 | | - "tags": ["javascript", "function", "rate-limiting", "utility"], |
938 | | - "author": "axorax" |
939 | | - }, |
940 | 913 | { |
941 | 914 | "title": "Random string", |
942 | 915 | "description": "Generates a random string of characters of a certain length", |
|
1098 | 1071 | ], |
1099 | 1072 | "tags": ["javascript", "localStorage", "storage", "utility"], |
1100 | 1073 | "author": "axorax" |
1101 | | - }, |
1102 | | - { |
1103 | | - "title": "Check bytes used", |
1104 | | - "description": "Checks the amount of bytes used in the localStorage.", |
1105 | | - "code": [ |
1106 | | - "const checkBytesUsed = () => {", |
1107 | | - " let spaceUsed = 0;", |
1108 | | - " for (let i = 0; i < localStorage.length; i++) {", |
1109 | | - " spaceUsed += localStorage.key(i).length + localStorage.getItem(localStorage.key(i)).length;", |
1110 | | - " }", |
1111 | | - " console.log(`Used space: ${spaceUsed} bytes`);", |
1112 | | - "};", |
1113 | | - "", |
1114 | | - "// Usage:", |
1115 | | - "checkBytesUsed();" |
1116 | | - ], |
1117 | | - "tags": ["javascript", "localStorage", "storage", "utility"], |
1118 | | - "author": "axorax" |
1119 | 1074 | } |
1120 | 1075 | ] |
1121 | 1076 | }, |
|
0 commit comments