On This Page
Function Utilities
The Function utilities provide helper functions for creating, wrapping, and optimizing functions in JavaScript. These utilities can help improve performance and simplify common function-related tasks.
noop
A function that does nothing (no operation).
Performance Optimization The primary purpose of
noop
is to provide a reusable, empty function that avoids the performance memory and performance overhead of creating new empty functions. This is particularly beneficial when used as a default value for optional function arguments or callbacks.
Example
wrapFunction
Ensures that the given value is a function. If it’s not, it returns a function that returns the value.
When to use Wrap function is helpful if you are unsure a function is defined but you want to call it. It can be simpler in some cases than type checking on a function.
Parameters
Name | Type | Description |
---|---|---|
x | any | The value to wrap |
Returns
A function that either calls the input function or returns the input value.
Example
memoize
Creates a memoized version of a function, caching its results for repeated calls with the same arguments.
Custom Hashing This implementation allows for a custom hashing function, enabling more control over how arguments are compared for caching. The default hashing function uses
JSON.stringify
and a hash code function, which may not be suitable for all use cases, especially with complex objects or circular references.
Parameters
Name | Type | Description |
---|---|---|
fn | function | The function to memoize |
hashFunction | function | (Optional) A function to generate a cache key from the arguments |
Returns
A new function that caches results based on its arguments.
Example
debounce
Creates a debounced version of a function, which will only invoke the function after it stops being called for a specified time period.
Parameters
Name | Type | Description |
---|---|---|
fn | function | The function to debounce |
options | number or object | Delay in milliseconds or options object |
Options Object
Name | Type | Default | Description |
---|---|---|---|
delay | number | 200 | Delay in milliseconds |
immediate | boolean | false | If true, trigger the function on the leading edge instead of the trailing edge |
Returns
A new debounced function.
Example
Cancellation The debounced function has a
cancel
method that can be used to cancel delayed invocations.
These function utilities provide powerful tools for manipulating and optimizing function behavior in JavaScript. They can help improve performance, reduce unnecessary computations, and simplify common programming patterns.