Skip to content

Instantly share code, notes, and snippets.

View ungarson's full-sized avatar
🎯
Focusing

Yo ungarson

🎯
Focusing
View GitHub Profile
@ungarson
ungarson / tryAsyncUntilOk.js
Last active October 13, 2021 08:56
Try asynchronous function several times until it returns result
async function tryAsyncUntilOk(func, times = 4, timeout = 1250) {
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
for (let i = 0; i < times; i++) {
try {
const res = await func();
if (typeof res !== "undefined") {
return res;
@ungarson
ungarson / Storage.js
Created October 10, 2021 13:55
Storage class for local storage management
export default class Storage {
static get(key) {
const value = localStorage.getItem(key);
if (!value) return undefined;
try {
return JSON.parse(value);
} catch {
localStorage.removeItem(key);
return undefined;
}
@ungarson
ungarson / optimize_site_speed_ways.txt
Last active October 20, 2020 11:49
Способы оптимизировать скорость загрузки сайта
Необходимые максимальные показатели (wi-fi, 200 mb/s, октябрь 2020):
Desktop:
1. FCP - 0.6s
2. Time To Interactive - 1.9s
3. Speed Index - 1.2s
4. LCP - 0.8s
5. CLS - 0.001s
6. Total Blocking Time - 0.1s
7. DomContentLoaded - 1.5s
@ungarson
ungarson / example.c
Created December 25, 2019 19:04
Struct with dynamic memory allocation in c
#include <stdio.h>
#include <stdlib.h>
struct course {
int mark;
char subject[30];
} mark;
struct course *ptr; // we don't allocate memory at that point because we don't know amount yet
int numberOfRecords;
@ungarson
ungarson / RangeHas.js
Last active July 21, 2019 06:39
Make range object by creating has method in the object's proxy in javascript
let range = {
start: 0,
finish: 10
}
range = new Proxy(range, {
has(target, property) {
if (property >= range.start && range.finish >= property) return true;
else return false;
}
@ungarson
ungarson / compose.js
Created July 20, 2019 06:09
Composition of asynchronous functions in javascript
export default function compose(...fns) {
return async x => {
let res = x;
for (const fn of fns) {
res = await fn(res);
}
return res;
}
}
@ungarson
ungarson / idGeneration.js
Created July 13, 2019 08:52
Passing an argument to a generator function in example of generating IDs
export default function* idGeneration(start = 0) {
while(true) {
const newID = yield start++;
if (typeof newID !== 'undefined') {
start = newID;
yield start++;
}
}
}
@ungarson
ungarson / RandomNameGenerator.js
Last active July 13, 2019 08:53
Random name generator in javascript using iterators
export default class RandomNameGenerator {
constructor(amountOfNames, lengthOfNames) {
this.amountOfNames = amountOfNames;
this.lengthOfNames = lengthOfNames;
this.names = new Array();
};
makeName(stringLength) {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
@ungarson
ungarson / generateIDS.js
Created July 7, 2019 12:00
ID generation in javascript
export default function* generateIDS(fromID) {
while (true) {
yield fromID++;
}
}
// Example of usage
// const generator = generateIDS(0);
// const MichaelID = generator.next().value;
// const DimaID = generator.next().value;
@ungarson
ungarson / AsyncCollector.js
Created July 7, 2019 11:38
Async collector in javascript
export default class AsyncCollector {
gotData = [];
constructor(timeout, ...funcs) {
return new Promise((resolve, reject) => {
const promisesOfFuncs = funcs.map(item => item());
Promise.all(promisesOfFuncs)
.then(values => this.gotData = values)
.then(() => {
setTimeout(() => {
resolve(this.gotData);