throttleit
Advanced tools
Comparing version 1.0.1 to 2.0.0
48
index.js
@@ -1,32 +0,24 @@ | ||
module.exports = throttle; | ||
function throttle(function_, wait) { | ||
let timeoutId; | ||
let lastCallTime = 0; | ||
/** | ||
* Returns a new function that, when invoked, invokes `func` at most once per `wait` milliseconds. | ||
* | ||
* @param {Function} func Function to wrap. | ||
* @param {Number} wait Number of milliseconds that must elapse between `func` invocations. | ||
* @return {Function} A new function that wraps the `func` function passed in. | ||
*/ | ||
return function throttled(...arguments_) { // eslint-disable-line func-names | ||
clearTimeout(timeoutId); | ||
function throttle (func, wait) { | ||
var ctx, args, rtn, timeoutID; // caching | ||
var last = 0; | ||
const now = Date.now(); | ||
const timeSinceLastCall = now - lastCallTime; | ||
const delayForNextCall = wait - timeSinceLastCall; | ||
return function throttled () { | ||
ctx = this; | ||
args = arguments; | ||
var delta = new Date() - last; | ||
if (!timeoutID) | ||
if (delta >= wait) call(); | ||
else timeoutID = setTimeout(call, wait - delta); | ||
return rtn; | ||
}; | ||
if (delayForNextCall <= 0) { | ||
lastCallTime = now; | ||
function_.apply(this, arguments_); | ||
} else { | ||
timeoutId = setTimeout(() => { | ||
lastCallTime = Date.now(); | ||
function_.apply(this, arguments_); | ||
}, delayForNextCall); | ||
} | ||
}; | ||
} | ||
function call () { | ||
timeoutID = 0; | ||
last = +new Date(); | ||
rtn = func.apply(ctx, args); | ||
ctx = null; | ||
args = null; | ||
} | ||
} | ||
module.exports = throttle; |
{ | ||
"name": "throttleit", | ||
"description": "Throttle a function", | ||
"version": "1.0.1", | ||
"keywords": [], | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/sindresorhus/throttleit.git" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^1.18.0" | ||
}, | ||
"license": "MIT", | ||
"component": { | ||
"scripts": { | ||
"throttle/index.js": "index.js" | ||
} | ||
}, | ||
"scripts": { | ||
"test": "mocha --reporter spec" | ||
} | ||
"name": "throttleit", | ||
"version": "2.0.0", | ||
"description": "Throttle a function to limit its execution rate", | ||
"license": "MIT", | ||
"repository": "sindresorhus/throttleit", | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"exports": { | ||
"types": "./index.d.ts", | ||
"default": "./index.js" | ||
}, | ||
"main": "./index.js", | ||
"types": "./index.d.ts", | ||
"sideEffects": false, | ||
"engines": { | ||
"node": ">=18" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
"keywords": [ | ||
"throttle", | ||
"rate", | ||
"limit", | ||
"limited", | ||
"rate-limit", | ||
"ratelimit", | ||
"throttling", | ||
"optimization", | ||
"performance", | ||
"function", | ||
"execution", | ||
"interval", | ||
"batch" | ||
], | ||
"devDependencies": { | ||
"ava": "^5.3.1", | ||
"xo": "^0.56.0" | ||
}, | ||
"xo": { | ||
"rules": { | ||
"unicorn/prefer-module": "off" | ||
} | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
4920
54
2
5
44
1