New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

benice

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

benice

Be nice to host, run "onIdle"

  • 1.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Benice - Be nice to your host!

Benice is a tiny set of browser performance estimation tools inspired by Dan Kaminsky's stupidly simple, genius "nice.js". It enables you to "be nice" to your surrounding hosting environment by running your logic only when the browser is estimated idle enough to handle it gracefully, thus reducing "hogging" resources, and slowing down fellow scripts. Benice also provides you with a rudimentary "Monitor" function that reports periodic estimations regarding how "idle" the browser is.

Installation

Benice is UMD compatible, and is available through:

  • NPM: npm install benice
  • Direct Download (Minified): http://benice.webwise.co.il/benice-min.js

onIdle

If your script is used on a 3rd party site, among many other scripts unknown to you, and you wish to defer your js code execution until the user's browser is relatively "idle", use:

var Benice = require('benice');

var stopWaiting = Benice.onIdle(function(){
    // Put your unobtrusive logic here
    console.log("Browser's Idle, nice time to show up!");
});

To cancel this wait for onIdle, simply call the function returned by `onIdle()':

stopWaiting();  // Wait is canceled, your code will never get executed

You can further tweak onIdle's behaviour using the following parameters:

onIdle(functionToExecute, detectionExpiration, resolutionSamples, sampleInterval, sampleThreshold, arg1, arg2, arg3...)

  • functionToExecute - The function to be run when an estimated "idle" state is successfully detected
  • detectionExpiration - A time interval (in milliseconds) after which functionToExecute will run regardless of a successful detection (defaults to 60000ms)
  • resolutionSamples - The number of samples requires to establish an "idle" estimation (defaults to 5 samples)
  • sampleInterval - The time interval used between samples (defaults to 150ms)
  • sampleThreshold - The sample threshold used to assume "idleness" (defaults to 50ms)
  • arg1, arg2, arg3... - The arguments to be used when calling functionToExecute.

For example:

The following call will exhaust detecting for browser idleness after 1 seconds:

onIdle(function(){ console.log('Detection attempt will exhaust in 1 second') }, 1000);

This one will require 10 samples to conclude that the browser is actually idle:

onIdle(function(){ console.log('After taking 10 samples, it is safe to estimate that the browser\'s idle') }, 60000, 10);

Monitor(functionToExecute, sampleInterval)

Monitor will call functionToExecute every sampleInterval passing it a coarse value ("samples") implying the browser's "idleness" state. 0 means "idle", X value (positive or negative) means X less idle. The simple method of establishing these values is explained in this Dan Kaminsky DEFCON talk.

You can abort a Monitor interval by calling the function it returns:

var stopMonitoring = Monitor(..);
stopMonitoring();

Examples

Page performance analyzer Page performance analyzer

Keywords

FAQs

Package last updated on 29 Aug 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc