UNPKG

@utilify/function

Version:

These utilities help with tasks such as debouncing, throttling, function composition, and managing async behavior, making it easier to handle function flow and optimize performance.

2 lines (1 loc) 1.72 kB
function n(n,t=1){return new Promise((async e=>{const r=[];for(let e=0;e<t;e++){const t=performance.now();await n();const e=performance.now();r.push(e-t)}e(function(n){return n.length?function(n){return n.reduce(((n,t)=>n+t),0)}(n)/n.length:0}(r))}))}function t(n,t=300){let e;return(...r)=>{clearTimeout(e),e=setTimeout((()=>n(...r)),t)}}function e(n,t){const e=new Map;return(...r)=>{const u=function(n){let t=5381;for(let e=0;e<n.length;e++)t=33*t^n.charCodeAt(e);return String(t>>>0)}(JSON.stringify(r));return e.has(u)||(e.set(u,n(...r)),t&&setTimeout((()=>e.delete(u)),t)),e.get(u)}}function r(n,t=300){let e;return(...r)=>{e||(e=setTimeout((()=>e=null),t),n(...r))}}function u(n,t){try{return n()}catch{return t()}}function o(n){let t=!1;return async(...e)=>{if(!t){t=!0;try{await n(...e)}catch(n){console.error("Callback execution error:",n)}finally{t=!1}}}}function c(){}function i(n){let t,e=!1;return(...r)=>(e||(e=!0,t=n(...r)),t)}function f(n,...t){return(...e)=>n(...t,...e)}function s(n,t,e){let r=0;return(...u)=>r<t&&(0===r&&setTimeout((()=>{r=0}),e),r++,n(...u),!0)}async function l(n){return new Promise((t=>setTimeout(t,n)))}function a(n,t,e){return(...r)=>n(...r)?t(...r):e(...r)}function m(...n){return t=>n.reduce(((n,t)=>t(n)),t)}function h(...n){return t=>n.reduceRight(((n,t)=>t(n)),t)}function y(n){Promise.resolve().then(n)}function g(n){return n}async function w(...n){return Promise.all(n.map((n=>n())))}function p(n,...t){return(...e)=>n(...e,...t)}export{n as benchmark,h as compose,t as debounce,y as defer,u as fallback,a as guard,g as identity,o as lock,e as memo,c as noop,i as once,w as parallel,f as partialLeft,p as partialRight,m as pipe,s as rate,l as sleep,r as throttle};