A utility that ensures a function is executed only once, returning the cached result on subsequent calls.
onetime is a JavaScript library that wraps a function to ensure it is called only once, caching and returning the initial result on all subsequent calls. It solves the problem of unintended repeated execution, such as in initialization routines or idempotent operations, by providing a simple, predictable utility.
JavaScript developers working in Node.js or browser environments who need to enforce single execution for functions, particularly in scenarios involving setup, lazy loading, or preventing side effects.
Developers choose onetime for its lightweight, non-invasive approach that avoids extending `Function.prototype`, along with features like error throwing on repeated calls and call count tracking for better control and debugging.
Ensure a function is only called once
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Unlike the once module, onetime avoids extending Function.prototype, preventing conflicts with other code or libraries, as emphasized in the README.
Provides a throw option to error on repeated calls and a callCount method to track invocations, helping developers enforce and debug single-execution behavior.
With a minimal API focused solely on single-call caching, it reliably prevents side effects from repeated execution without adding unnecessary complexity.
It only caches the first call's result permanently, lacking features like cache invalidation or resetting, which can be restrictive for dynamic applications.
For a simple utility that can be replicated with a few lines of JavaScript, adding this library may introduce bundle size concerns and dependency management overhead.