Skip to content

Commit

Permalink
Update the readme a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Krb686 committed Nov 4, 2015
1 parent 2f2c317 commit 4a3ac14
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 51 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.settings
.project
node_modules
node_modules
misc
129 changes: 79 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# nanoTimer
# Current Version - 0.3.10
# nanotimer
# Current Version - 0.3.11

![](https://api.travis-ci.org/Krb686/nanoTimer.png)

Expand All @@ -9,25 +9,25 @@ A much higher accuracy timer object that makes use of the node.js [hrtime](http:

The nanotimer recreates the internal javascript timing functions with higher resolution.

##Note
## Note

- 1) With the normal timing functions, instead of dealing with the obscurities of multiple setTimeout and
setInterval calls, now there is a concrete timer object, each of which can handle exactly 1 timeOut and
- 1) With the normal timing functions, instead of dealing with the obscurities of multiple setTimeout and
setInterval calls, now there is a concrete timer object, each of which can handle exactly 1 timeOut and
setInterval task. This also means a reference is not needed to clear an interval since each timer object is
unique.

- 2) Timer objects use the non-blocking feature **setImmediate** for time counting and synchronization. This requires node v0.10.13 or greater

- 3) Errors in timing are also non-cumulative. For example, when using the setInterval command, the timer counts
and compares the time difference since starting against the interval length specified and if it has run past the interval,
- 3) Errors in timing are non-cumulative. For example when using the setInterval command, the timer counts
and compares the time difference since starting against the interval length specified and if it has run past the interval,
it resets. If the code had an error of 1 millisecond delay, the timer would actually count to 1001 milliseconds before resetting, and that
1 millisecond error would propagate through each cycle and add up very quickly! To solve that problem, rather than resetting
the interval variable each cycle, it is instead incremented with each cycle count. So on the 2nd cycle, it compares to 2000 milliseconds,
and it may run to 2001. Then 3000 milliseconds, running to 3001, and so on. This is only limited by the comparison variable potentially overflowing, so I
somewhat arbitrarily chose a value of 8 quadrillion (max is roughly 9 quadrillion in javascript) before it resets. Even using nanosecond resolution however,
the interval variable each cycle, it is instead incremented with each cycle count. So on the 2nd cycle, it compares to 2000 milliseconds,
and it may run to 2001. Then 3000 milliseconds, running to 3001, and so on. This is only limited by the comparison variable potentially overflowing, so I
somewhat arbitrarily chose a value of 8 quadrillion (max is roughly 9 quadrillion in javascript) before it resets. Even using nanosecond resolution however,
the comparison variable would reach 8 quadrillion every 8 million seconds, or every 93.6ish days.

##Usage
## Usage

```js

Expand All @@ -38,9 +38,9 @@ var timerA = new NanoTimer();

```

Each NanoTimer object can run other functions that are already defined. This can be done in 2 ways, either with a literal function object, or with a function declaration.
Each nanotimer object can run other functions that are already defined. This can be done in 2 ways; with either a literal function object, or with a function declaration.

### By function object
### by function object
```js
var NanoTimer = require('nanotimer');
var timerObject = new NanoTimer();
Expand All @@ -66,7 +66,7 @@ var NanoTimer = require('nanotimer');

function main(){
var timerObject = new NanoTimer();

var microsecs = timerObject.time(countToOneBillion, '', 'u');
console.log(microsecs);
}
Expand All @@ -80,8 +80,8 @@ function countToOneBillion(){

main();
```
##Full example

## Full example

```js
var NanoTimer = require('nanotimer');
Expand All @@ -91,10 +91,10 @@ var count = 10;

function main(){
var timer = new NanoTimer();

timer.setInterval(countDown, '', '1s');
timer.setTimeout(liftOff, [timer], '10s');



}
Expand All @@ -112,7 +112,7 @@ function liftOff(timer){
main();
```

### In the above example, the interval can also be cleared another way rather than having to pass in the timer object to the liftOff task.
### In the example above, the interval can also be cleared another way rather than having to pass in the timer object to the liftOff task.
Instead, it can be done by specifying a callback to setTimeout, since the timer object will exist in that scope. Like so:

```js
Expand All @@ -123,10 +123,22 @@ timer.setTimeout(liftOff, '', '10s', function(){


## .setTimeout(task, args, timeout, [callback])
* Calls function 'task' with argument(s) 'args' after specified amount of time, 'timeout'.
* timeout, specified as a number plus a letter concatenated into a string. ex - '200u', '150n', '35m', '10s'.
* callback is optional. If it is specified, it is called when setTimeout runs it's assigned task, and it is sent a parameter that
tells the actual amount of time that passed before the specifed task was run, in nanoseconds.
* **task**
* The function or task to run. Can be either a literal function object or reference to a function declaration.
* **args**
* An array of arguments to pass to **task**, or an empty string, **''** , if there are none. Note, even a single argument must be passed as an element in an array.
* Ex. ['someString', 5, someVariable]
* **timeout**
* The amount of time to wait before calling **task**. This is a string containing a non-zero integer concatenated with one of 4 possible unit specifiers.
* Unit specifiers are:
* **s** = seconds
* **m** = milliseconds
* **u** = microseconds
* **n** = nanoseconds
* Ex. '500s', '37u', '45n'
* **[callback]]**
* The optional callback function to execute after the timeout has triggered.


```js
console.log("It's gonna be legen-wait for it...");
Expand All @@ -139,11 +151,25 @@ function dary(){
```

## .setInterval(task, args, interval, [callback])
* Repeatedly calls function 'task' with arguments 'args' after every interval amount of time. If interval is specified as 0, it will run as fast as possible!
* This function is self correcting, error does not propagate through each cycle, as described above.
* interval, specified as a number plus a letter concatenated into a string. ex - '200u', '150n', '35m', '10s'.
* callback is optional, and is only called once the interval is cleared.

* **task**
* The function or task to run. Can be either a literal function object or reference to a function declaration.
* **args**
* An array of arguments to pass to **task**, or an empty string, **''** , if there are none. Note, even a single argument must be passed as an element in an array.
* Ex. ['someString', 5, someVariable]
* **interval**
* The interval of time to wait between each call of **task**. This is a string containing a non-zero integer concatenated with one of 4 possible unit specifiers.
* Unit specifiers are:
* **s** = seconds
* **m** = milliseconds
* **u** = microseconds
* **n** = nanoseconds
* Ex. '500s', '37u', '45n'
* **Note**:
* If **interval** is specified as 0, the timer will run **task** as fast as possible.
* This function is self correcting, error does not propagate through each cycle, as described above.
* **[callback]]**
* The optional callback function to execute after the timeout has triggered.

```js
timerA.setInterval(task, '100m', function(err) {
if(err) {
Expand All @@ -153,10 +179,21 @@ timerA.setInterval(task, '100m', function(err) {
```

## .time(task, args, format, [callback])
* Returns the amount of time taken to run function 'task', called with arguments 'args'.
* format specifies the units time will be returned in. Options are 's' for seconds, 'm' for milliseconds, 'u' for microseconds,
and 'n' for nanoseconds. if no format is specified, returns the default array of [s, n] where s is seconds and n is nanoseconds.
* callback is optional
* **task**
* The function or task to run. Can be either a literal function object or reference to a function declaration.
* **args**
* An array of arguments to pass to **task**, or an empty string, **''** , if there are none. Note, even a single argument must be passed as an element in an array.
* Ex. ['someString', 5, someVariable]
* **format**
* A single unit specifier indicating the format of the data to be returned.
* Unit specifiers:
* **s** = seconds
* **m** = milliseconds
* **u** = microseconds
* **n** = nanoseconds
* Ex. '500s', '37u', '45n'
* **[callback]**
* The optional callback function to execute after the time call has triggered.

### Synchronous Example:
```js
Expand All @@ -169,12 +206,14 @@ function doMath(){

```

### Asynchronous Use: (yes, it can time asynchronous stuff! here's how)
### Asynchronous Use:
To time something asynchronous, you only need to do these 3 things:
1. create a small wrapper function around the asynchronous task.
2. make the wrapper function take *callback* as a parameter.
3. manually call the *callback* parameter inside the callback of the asynchronous task.

In order to time something asynchronous, it's no surprise that the timer object must somehow be notified whenever that asynchronous task finishes.
To do that, you must have your function to be timed accept a callback as a parameter, and manually call that callback (to the timer function) inside of the asynchronous task's
callback. It's essentially a chain of callbacks, which is probably already familiar to you. Here's an example that times how long it takes to read a file.
Suppose you're using node.js's fs.ReadFile, which is asynchronous, then create a wrapper like so:
It's essentially a chain of callbacks, which is probably already familiar to you. Here's an example that times how long it takes to read a file.
Suppose you're using node.js's fs.ReadFile, which is asynchronous, then create a wrapper like so:
```js
var NanoTimer = require('nanotimer');
var fs = require('fs');
Expand All @@ -190,15 +229,12 @@ function loadFile(callback){
fs.readFile('testReadFile.js', function(err, data){
if(err) throw err;
console.log(data);

callback();
});
}
```

Once again, just two changes from normal. First, your function to be timed, in this case 'loadFile' (which is just a proxy function to perfom fs.readFile) must
accept a callback parameter. Second, in the callback of whatever asynchronous task is being performed inside the proxy, the callback passed in must be called after everything
is finished. That callback that you call manually immediately takes the 2nd reference time, and then calls your callback specified in timer.time, ending the process.

## .clearInterval()
* Clears current running interval
Expand All @@ -212,10 +248,10 @@ timer.clearInterval();
timer.clearTimeout();
```

#Logging
# Logging
* Added preliminary logging feature. If a timer is created by passing in 'log', it will enable verbose logging from the timer, so you can
figure out the real amount of time being taken for setTimeout or setInterval
* Currently only works on setInterval, and displays the 'cycle time', or real interval time to demonstrate how error does not propagate. This will
* Currently only works on setInterval, and displays the 'cycle time', or real interval time to demonstrate how error does not propagate. This will
be further expanded on.

# Tests
Expand Down Expand Up @@ -245,10 +281,3 @@ Below is a test case with setInterval set to 1 second.










0 comments on commit 4a3ac14

Please sign in to comment.