Skip to content

Instantly share code, notes, and snippets.

View dSalieri's full-sized avatar
😒
Dark guardian always there!

dSalieri

😒
Dark guardian always there!
View GitHub Profile
@dSalieri
dSalieri / renderNextFrame.js
Last active September 12, 2021 08:01
Invokes callback function next frame
function renderNextFrame(callback){
if(typeof callback !== "function") return;
requestAnimationFrame(() => {
requestAnimationFrame(() => {
callback();
});
});
}
@dSalieri
dSalieri / eventLoop.js
Last active September 14, 2021 23:07
This is a picture how event loop works.
document.addEventListener("click", () => {
setTimeout(() =>{
Promise.resolve(13).then((value) => console.log(value));
console.log("click1");
})
})
document.addEventListener("click", () => {
setTimeout(() =>{
Promise.resolve(17).then((value) => console.log(value));
console.log("click2");
@dSalieri
dSalieri / rand.js
Created September 19, 2021 08:05
Pseudorandom is based on Math.random function
function rand(start, end){
return start + (Math.round(Math.random() * Math.abs(start - end)));
}
@dSalieri
dSalieri / shallowSetEquality.js
Created September 20, 2021 04:53
Test on equality of sets
function shallowSetEquality(set1, set2){
if(!(set1 instanceof Set) || !(set2 instanceof Set))
throw Error("One of arguments or both is/are not instance of Set");
if(set1 === set2) return true;
let it1 = set1[Symbol.iterator]();
let it2 = set2[Symbol.iterator]();
while(true){
let {value: v1, done: d1} = it1.next();
let {value: v2, done: d2} = it2.next();
if(d1 === true || d2 === true) {
@dSalieri
dSalieri / shallowMapEquality.js
Created September 20, 2021 08:43
Test on equality of maps
function shallowMapEquality(map1, map2){
if(!(map1 instanceof Map) || !(map2 instanceof Map))
throw Error("One of arguments or both is/are not instance of Map");
if(map1 === map2) return true;
let it1 = map1[Symbol.iterator]();
let it2 = map2[Symbol.iterator]();
while(true){
let {value: v1, done: d1} = it1.next();
let {value: v2, done: d2} = it2.next();
if(d1 === true || d2 === true) {
@dSalieri
dSalieri / objectsEquality.js
Last active September 22, 2021 06:22
Compares two objects on equality
/// Compares two objects and returns true or false
/// No protection for recursive structure of objects, don't compare recursive objects
/// Comparation doesn't provide exotic objects like Set, Map or Date, you should realize them by yourself if you want compare it too
/// Below there is demonstration how to realize and include exotic objects into main algorithm
function objectsEquality(specialTypes) {
specialTypes = sortOf(specialTypes) === "object" ? specialTypes : {};
return function equality(o1, o2, options) {
/// if arguments that must be objects aren't them, value is returned null
if ([o1, o2].some((i) => typeof i !== "object")) return null;
/// Objects are equal, if objects have same reference to object
@dSalieri
dSalieri / index.md
Last active January 12, 2022 19:45
Работа формальных параметров через спецификацию метода-ловушки construct у объекта Proxy

Вопрос: Почему мы должны применить деструктуризацию параметра чтобы получить массив который передаем при создании.

Код:

const trapsObject = {
    construct(target, [args]){
        console.log(args); /// [1,2,3]
        return {};
    }
};
const Exo = new Proxy(Array, trapsObject);
@dSalieri
dSalieri / index.md
Last active January 31, 2022 23:06
Сравнение операторов || и ??; как выглядит это внутри

Как выглядит это внутри:

Оператор ||:

LogicalORExpression : LogicalORExpression || LogicalANDExpression

1. Let lref be the result of evaluating LogicalORExpression.
2. Let lval be ? GetValue(lref).
3. Let lbool be ! ToBoolean(lval).
4. If lbool is true, return lval.
5. Let rref be the result of evaluating LogicalANDExpression.
@dSalieri
dSalieri / _index.md
Last active July 8, 2022 02:04
Promise.prototype.finally

Реализация Promise.prototype.finally
Имеет зависимость от SpeciesConstructor

@dSalieri
dSalieri / index.md
Last active August 17, 2022 17:05
Tracking user activation (whatwg)

Этот текст был составлен как проблема и помощь в понимании некоторых вещей по спецификации whatwg. Текст проблемы не был принят во внимание и проигнорирован, а мое объяснение было посоветовано разместить в своем блоге.

Текст ниже является объясняющей частью (можете воспользоваться переводчиком, перевод будет сносным, я смотрел). Не удалять же мне эти интересные находки, может кому-то это будет полезным.

I noticed that Chrome APIs gives ~5000ms for APIs that using transient activation in their algorithms for successfull completion

/// measuring only for Chrome
(() => {
    let timerId = null;
    let start = null;