@remvst/optimization
v1.5.2
Published
![Build status](https://github.com/remvst/optimization/actions/workflows/check.yaml/badge.svg)
Downloads
306
Readme
optimization
Various optimizations utils.
Installation
npm install @remvst/optimization
ReusablePool
// Create a pool
const pool = new ReusablePool(() => createComplexObject());
// (Optional) Prepare a few objects ahead of time
pool.prepare(10);
// Retrieve an object from the pool
const myObject = pool.take();
// Give the object back to the pool
pool.give(myObject);
ThrottledValue
// Create a lazy reference
const expensiveObject = lazy(() => createComplexObject());
// Use the reference
expensiveObject.get().doStuff();
// (Optional) Provide a cleanup mechanism
const expensiveObjectWithReset = lazy(
() => createComplexObject(),
(obj) => obj.destroy(),
);
expensiveObject.reset();
ThrottledValue
const throttled = new ThrottledValue(
1, // Compute every 1s
() => performance.now() / 1000, // Provide the current time
() => doAnExpensiveCalculation(), // Provide the computed value
);
// First call will trigger doAnExpensiveCalculation()
const currentValue = throttled.get();
// Second call will only trigger doAnExpensiveCalculation() if 1s has passed
const newValue = throttled.get();
// (Optional) Reset when relevant
throttled.reset();
ValueChangeHelper
const languageValue = new ValueChangeHelper<string>();
// requiresUpdate() will only return true if the value is new
// or different from the previous one
if (languageValue.requiresUpdate(navigator.language)) {
translateEverything(languageValue.get());
}
// (Optional) Reset when relevant
languageValue.reset();
RollingAverage
const rollingAvg = new RollingAverage(10);
// On each frame, record the framerate
function onFrame() {
rollingAverage.record(performance.now(), framerate);
}
// Get the average framerate over the last 10 frames
console.log(rollingAverage.average);