speedometer
Advanced tools
Comparing version 0.1.3 to 0.1.4
52
index.js
@@ -1,35 +0,35 @@ | ||
var tick = 1; | ||
var maxTick = 65535; | ||
var resolution = 4; | ||
var tick = 1 | ||
var maxTick = 65535 | ||
var resolution = 4 | ||
var inc = function() { | ||
tick = (tick + 1) & maxTick; | ||
}; | ||
tick = (tick + 1) & maxTick | ||
} | ||
var timer = setInterval(inc, (1000 / resolution) | 0); | ||
if (timer.unref) timer.unref(); | ||
var timer = setInterval(inc, (1000 / resolution) | 0) | ||
if (timer.unref) timer.unref() | ||
module.exports = function(seconds) { | ||
var size = resolution * (seconds || 5); | ||
var buffer = [0]; | ||
var pointer = 1; | ||
var last = (tick-1) & maxTick; | ||
var size = resolution * (seconds || 5) | ||
var buffer = [0] | ||
var pointer = 1 | ||
var last = (tick-1) & maxTick | ||
return function(delta) { | ||
var dist = (tick - last) & maxTick; | ||
if (dist > size) dist = size; | ||
last = tick; | ||
return function(delta) { | ||
var dist = (tick - last) & maxTick | ||
if (dist > size) dist = size | ||
last = tick | ||
while (dist--) { | ||
if (pointer === size) pointer = 0; | ||
buffer[pointer] = buffer[pointer === 0 ? size-1 : pointer-1]; | ||
pointer++; | ||
} | ||
while (dist--) { | ||
if (pointer === size) pointer = 0 | ||
buffer[pointer] = buffer[pointer === 0 ? size-1 : pointer-1] | ||
pointer++ | ||
} | ||
if (delta) buffer[pointer-1] += delta; | ||
if (delta) buffer[pointer-1] += delta | ||
var top = buffer[pointer-1]; | ||
var btm = buffer.length < size ? 0 : buffer[pointer === size ? 0 : pointer]; | ||
var top = buffer[pointer-1] | ||
var btm = buffer.length < size ? 0 : buffer[pointer === size ? 0 : pointer] | ||
return buffer.length < resolution ? top : (top - btm) * resolution / buffer.length; | ||
}; | ||
}; | ||
return buffer.length < resolution ? top : (top - btm) * resolution / buffer.length | ||
} | ||
} |
{ | ||
"name": "speedometer", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"repository": "git://github.com/mafintosh/speedometer", | ||
@@ -5,0 +5,0 @@ "description": "simple speed measurement in javascript", |
@@ -5,3 +5,5 @@ # speedometer | ||
npm install speedometer | ||
``` | ||
npm install speedometer | ||
``` | ||
@@ -11,15 +13,15 @@ ## Usage | ||
``` js | ||
var speedometer = require('speedometer'); | ||
var fs = require('fs'); | ||
var speedometer = require('speedometer') | ||
var fs = require('fs') | ||
// Let's measure how fast we can read from /dev/urandom | ||
var speed = speedometer(); | ||
var stream = fs.createReadStream('/dev/urandom'); | ||
var speed = speedometer() | ||
var stream = fs.createReadStream('/dev/urandom') | ||
stream.on('data', function(data) { | ||
// Simply call speed with the amount of bytes transferred | ||
var bytesPerSecond = speed(data.length); | ||
// Simply call speed with the amount of bytes transferred | ||
var bytesPerSecond = speed(data.length) | ||
console.log(bytesPerSecond+' bytes/second'); | ||
}); | ||
console.log(bytesPerSecond+' bytes/second') | ||
}) | ||
``` | ||
@@ -33,3 +35,3 @@ | ||
``` js | ||
var speed = speedometer(20); // uses a 20s buffer instead | ||
var speed = speedometer(20) // uses a 20s buffer instead | ||
``` | ||
@@ -36,0 +38,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2965
39