Skip to content

Instantly share code, notes, and snippets.

Avatar
⏱️
Performance..

James Hill jahilldev

⏱️
Performance..
View GitHub Profile
@jahilldev
jahilldev / is-target-element.js
Last active Jun 1, 2022
Function to match an element via a target selector in the provided tree. Useful for event delegation.
View is-target-element.js
function isTargetElement(element, selector) {
let target = element;
while (target) {
if (target?.matches(selector)) {
break;
}
target = target?.parentNode;
}
@jahilldev
jahilldev / get-hash-from-value.js
Created May 30, 2022
Generate a non-secure reproducible hash from an input seed value (not suitable for passwords, or sensitive data)
View get-hash-from-value.js
function getHash(value, length = 16) {
let hash = 0;
for (let index = 0; index < value.length; index++) {
hash = (hash << 5) - hash + value.charCodeAt(index);
hash = hash & hash;
}
hash = Math.abs(hash);
View check-prime.js
function isPrimeNumber(value) {
for(let i = 2; i < value; i++) {
if(value % i === 0) {
return false;
}
return value > 1;
}
return false;
@jahilldev
jahilldev / tiny-debounce.js
Last active May 12, 2022
Tiny JavaScript debounce function
View tiny-debounce.js
function debounce(callback, frequency = 250, timer = null) {
return (...args) => (
clearTimeout(timer), (timer = setTimeout(callback, frequency, ...args))
);
}