npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

framerate-optimizer

v0.2.0

Published

Library for tracking and iteratively improving framerate over time.

Downloads

1,263

Readme

js-framerate-optimizer

npm version build lgtm code quality

Library for tracking and iteratively improving framerate over time inspired by the Babylon.SceneOptimizer.

The terms "increase work" and "decrease work" are synonymous with "improve quality" and "improve performance" respectively.

Use

import { Optimizer } from '.../Optimizer.js';

// wait for x milliseconds
function wait(ms) {

    const start = window.performance.now();
    while (window.performance.now < start + ms) {}

}

// optimize the work time until we reach the target framerate
const optimizer = new Optimizer();
optimizer.addOptimization(delta => {

    workTime += delta < 0 ? -1 : 1;
    return true;

});

// work loop
let workTime = 500;
(function loop(){

    optimizer.update();
    wait(workTime);
    requestAnimationFrame(loop);

})();

How it Works

The optimizer tracks the amount of time spent between frames or between calls to begin and end and calculates the difference between the target amount of time to spend and the actual time spent on the last frame. After the specified amount of time has passed the average time spent is calculated and the framerate the amount of work is either increased or decreased depending on whether or not the time spent was above or below the target. The amount of work is adjusted by iteratively calling prioritized optimizations and sampling framerate until the target work time is met.

API

Optimizer

.enabled

enabled = true : Boolean

Getter and setter for enabling or disabling the optimizer. Elapsed time is reset on reenable.

.completed

completed : Boolean

Whether or not the optimizer has stopped iterating and sampling the framerate.

.constructor

constructor( options : Object )

All options can be accessed and modified on Optimizer.options.

options.targetMillis

The target milliseconds to hit in the enclosed code block. This cannot be less than 16.66... because browser caps the framerate to 60 frames per second. If this is less than that the optimizer will continually decrease quality to try to get performance up.

Defaults to 16.66....

options.targetFramerate

The target framerate to hit. This overrides targetMillis if it is set.

Defaults to null.

options.interval

How often to perform a check sample the average framerate to try to optimize in milliseconds.

Defaults to 500 or half a second.

options.maxFrameSamples

At most how many frames can run before an optimization should occur. This is useful when code may not run consistently but runs as needed, meaning that checking after a fixed amount of time might mean that an inconsistent or no actual iterations has occurred. Interval should probably be set to Infinity in this case.

Defaults to Infinity.

options.waitMillis

The amount of time to wait between sampling frames for a new optimization. This is useful when an optimization may cause the framerate to drop for a frame or two -- such as with allocating a lot of new memory or new webgl render targets.

Defaults to 0;

options.maxWaitFrames

At most how many frames to wait for.

Defaults to Infinity;

options.margin

How far outside of the target framerate must be in order for an optimization to occurs.

Defaults to 0.05 or 5%.

options.continuallyRefine

By default the optimizer will stop trying to optimize and optimization the page once the target framerate is hit or no more optimizations can run and will never try to improve quality of the page again after the first iteration.

This option allows the optimizer to ping pong between improving performance and quality continually to keep a steady framerate. Note that this may not be a good option when using "expensive" optimizations that may stall the frame for a moment, like compiling shaders.

Defaults to false.

options.increaseWork

Whether the optimizer should ever try to increase the amount of work done at the cost of framerate. After the quality improvements are made the optimizer tries to improve the framerate until the target framerate is met.

Defaults to false.

.dispose

dispose() : void

Removes window events that the optimizer listens for, including window "blur" and "focus". It is expected that the optimizer is no longer used after this.

.addOptimization

addOptimization( optimization : Optimization, priority = 0 : Number ) : void

Takes an instance of a Optimization (described below) and a priority by which to call the optimization. Optimizations are optimized iteratively until each priority level has been completely optimized then it will move to the next one. When improving quality the lowest priority level is started with to improve quality. The highest is started with when improving performance.

Expensive but unnecessary work should be optimized at a high priority level. Critical functionality should be optimized at a low priority level.

.begin

.end

begin() : void
end() : void

begin an end are used to encapsulate the code block to be optimized. Any optimizations being used should affect the performance of the code within that code block.

optimizer.begin();

// ... code to optimize ...

optimizer.end();

.update

update() : void

update takes the place of both begin() and end() and should be called once per frame to optimize the full frame time.


function loop() {

    optimizer.update();

    // ... code to optimize

    requesAnimationFrame(loop);

}

.addSample

addSample( time : Number ) : void

Call instead of begin, end, or update if you'd like to use a separate time-measuring mechanism that cannot be achieved with the aforementioned function, such as when measuring GPU work time with the WebGL disjoint_timer_query.

.restart

restart() : void

Restarts optimization from the beginning.

This should be called if something on the page changed icantly which might give more room for improvement, such as significantly resizing the browser window with a WebGL app.

Optimization

An optimization base class for creating a reusable optimization to add to the Optimizer.

.optimize

optimize( delta : Number, optimizer : Optimizer )

The optimize function is called whenever an optimization should take place, either to improve performance or quality. The delta argument is the amount of milliseconds difference between the target framerate and the current framerate. A negative value means that less work should be done to hit the target.

The optimize function must return true if the setting was optimized and 'false' if no optimization could occur, ie the setting being optimized could not be turned down any further or is at the lowest acceptable setting.

The second argument optimizer is the optimizer that is running the optimization.

SimpleOptimization

A extension of Optimization that abstracts some of the basic functionality needed to implement it.

.canIncreaseWork / .canDecreaseWork

canIncreaseWork() : Boolean
canDecreaseWork() : Boolean

Functions that should return true if the optimizer can increase or decrease work. If they return true the associated optimization function will be called.

.increaseWork / .decreaseWork

increaesWork( delta : Number ) : void
decreaseWork( delta : Number ) : void

Functions to increase or decrease work and optimize the frame if needed. These are called if the associated canIncreaseWork or canDecreaseWork return true.