1.5.2 • Published 9 months ago

@remvst/optimization v1.5.2

Weekly downloads
-
License
UNLICENSED
Repository
-
Last release
9 months ago

Build status

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);
1.5.2

9 months ago

1.5.1

9 months ago

1.5.0

9 months ago

1.4.0

9 months ago

1.3.0

9 months ago

1.2.0

1 year ago

1.1.0

1 year ago

1.0.1

2 years ago

1.0.0

2 years ago